Learn Working with Files and Directories in Red Hat Linux

0
2564
Red-Hat-Linux-Working-with-Files-and-Directories

Red-Hat-Linux-Working-with-Files-and-Directories

To reiterate, everything in Linux is a file. Files are stored in logical containers (actually, another type of files) called directories. So, we need to know how to work with files and directories. That will be the subject of this article. Wish you nice reading.

Determining the Type of a File
Before working with a file, we need first to determine the type of the file: whether it is a regular text file, binary executable, directory, soft link, block special file, character special file, etc. For this purpose, the file command was written.
Determine the Type of file

Listing the Contents of a Directory
After determining where we are (the current directory) using the pwd command, and changing to a directory (if we need to do) using the cd command, we need to display the contents of a directory. The ls command does this for us.

Syntax
ls [OPTIONS] [ARGUMENTS]

Examples

  • To list the contents of the current directory, use ls without arguments.

1

  • To list the contents of the directory called network-scripts:

2

  • To see a long listing of the /etc/ssh directory, use the –l option (small L for long)

3

  • To list the files with human-readable file sizes, use the –h option.

4

  • To list the files, sorted by their last modification date/time (from oldest to newest), use the –ltr option.

5

  • To list all files, use the –a option:

6

Note
Files and directories whose names start with a dot .are considered hidden; they are not listed by default.

  • To list directories recursively, use the –R option.

7

Using –R causes the ls command to recurse into all sub-directories.

  • To display info about the directory itself (not its contents), use the –ld options.

8

Creating a Directory
To create a directory, use the command mkdir (make directory)

Syntax
mkdir [DIRNAME] …

Examples

  • To create a directory named newdata under /opt:

9

  • To create multiple directories with names Jan, Feb, Mar, …, Dec

10

  • To create a directory and create its parent(s) if they don’t exist, use the –p option.

11

If you try to execute this command without –p, the shell will complain, firing the following error:
12

Removing an Empty Directory
To remove one or more empty directories (that has no files or sub-directories inside), use the rmdir command.
13

If you try this command with a non-empty directory, you will get an error:
14

Removing Any Directory
To remove non-empty (and empty) directories, use the rm –r command.
15

Note
To enforce deletion without prompting, use the –f option with the rm command.

Creating Files
There are several ways to create files. The one we are going to discuss in this section is using the touch command.

Syntax
touch [FILENAME] …

Examples

  • The following will create an empty file /opt/test1.txt

16

  • To create files day00, day01, …, day31:

17

Copying Files and Directories
To copy one or more files from a given location (source) to another (destination), the cp command is used.
cp FILE1 FILE2
cp FILE1 DESTINATION/
cp FILE1 FILE2 … FILEn DESTINATION/
The first form of the command copies a file from one location to another using a new name for the resulting copy.
The second form copies the file to a destination directory. The new file at the destination directory will have the same name as the original one.
The third form copies several files to a destination directory.

Note
If multiple source files are to be copies at a time, the destination MUST be a directory.

Coping a Directory
To copy a directory, use the same command cp with –r (recursive) option

Moving and Renaming Files and Directories
When copying files and directories, the source file(s) and/or directories remain unaffected. On the other hand, moving files and directories will remove the source(s). You can think of it like copying then removing.
The mv command moves a file or directory from its current location to another. If the
mv FILE1 FILE2 … FILEn DESTINATION/
This will move one or more files from its/their current location to (another) destination directory.
mv OLDNAME NEWNAME
This renames a file or directory.

Removing Files
The rm command is used to remove files.

Summary
In this article, we have discussed working with files and directories.

  • The file command is used to determine the type of a file.
  • The ls command lists the contents of directories.
  • The mkdir command creates directories.
  • To remove an empty directory, use rmdir command. For non-empty directories, use rm –r
  • To create empty files, we can use the touch command.
  • The cp command is used to copy files and directories.
  • The mv command moves or renames files and directories.

In the next article, we will learn how to obtain help on need. See you there.

LEAVE A REPLY

Please enter your comment!
Please enter your name here