File commands in Linux
In this topic, you will learn about various commands required for managing files and directories in Linux.
Displaying the contents of file
“cat” command is used to display the contents of file. “tac” command is also used to display contents of file but from bottom to top.
Here is the syntax of cat command.
$ cat <file-name>
To number the lines in the output, you can use below command.
$ cat -n <file-name>
“rev” command displays the file contents but each line is reversed.
Deleting entire data from a file
To make the file empty, we can use any of the below command.
- : > file1
- > file1
- cat /dev/null > file1
- truncate -s 0 file1
Creating a directory using mkdir command
$ mkdir <directory-name>
Deleting a file or a directory using rm command
$ rm <file-name-or-directory-name>
To delete the files recursively in a directory, you can use below command.
$ rm -r <directory-name>
To prompt the user before deleting file, you can use below syntax.
$ rm -i <file-name>
Copying files using cp command
Here is the syntax of cp command.
cp <source-file> <destination-file>
If you want to preserve, attributes of file like mode, ownership etc, you can use below command.
$ cp -p <source-file> <destination-file>
If you want to copy files interactively, you can use below command. Before each copy operation, you will be asked for the permission.
$ cp -i <source-file> <destination-file>
Renaming and moving files using mv command
You can use below command to rename a file.
$ mv <old-file> <new-file>
You can use below command to move a file.
$ mv <source-file> <destination-file>
You can use below command to move files interactively.
$ mv -i <source-file> <destination-file>
You can use below command to move a files forcibly. If the destination file exists, it will be overwritten.
$ mv -f <source-file> <destination-file>
Listing files and directories using ls command.
“ls” command shows the list of file names in the directory. But if you want to know other details of the file like its size, permission etc, you can run below command.
$ ls -l
Here is the list of other ls Options
- -a : This option is used to show all files including hidden ones.
- -R : This option is used to list files recursively from sub-directories.
- -r : This option is used to list files in reverse order.
- -S : This option is used to sort the files by size.
- -1 : This option is used to list files separated by new line character.
- -m : This option is used to list files separated by coma (,)
- -Q : This option is used to list files in double quotes (” “)
Understanding the output of ls -l command
Below image shows the sample output of ls command.
First line shows total file system blocks required to store all files and sub directories in current directory. After that , there is a line for each file.
The 1st character in the each line can take any of the below value.
- “-” means it is a regular file
- “l” means it is a link file
- “d” means it is a directory
- “c” means it is a character device
- “b” means it is a block device
- “p” means it is a pipe
- “C” means high performance file
- “s” means socket file
- “?” means other unknown file
After that permission attributes are listed for each file for owner, group and others. Then the count of number of hard links is displayed. After that owner and group of file is displayed. Then file size, access time and file name is displayed.
“file” command is used to display the type of file (e.g. ASCII file, BZ2 file etc)
$ file <file-name>
Viewing file attributes using stat command
“stat” command is used to display file attributes.
Changing attributes of file using touch command
“touch” command is used to change the file access and modification time.
“-a” option stands for access time.
“-m” option stands for modification time.
Soft links and hard links to files
Link is nothing but the file pointing to another file. There are 2 types of links.
- Soft Link – inode numbers are different. If you delete the original file, soft link file will fail.
- Hard Link – inode numbers are same. Even if you delete the original file, hard link file will work.
You can use below syntax to create soft link.
ln -s <original-file-path> <link-file>
You can use below syntax to create hard link.
ln <original-file-path> <link-file>
pushd and popd commands
pushd and popd commands are used to switch between directories very easily.
pushd command pushes the directory on the top of stack and also changes directory.
For example – Below command will change the directory to “d1” and also push the “d1” to top of the directory stack.
Directory stack will look like d1 ~
Note that “~” indicates that home directory is also pushed to stack by default. So right now we are in “d1”
Now let us say you want to change to directory to “d2”
Directory stack will look like d2 d1 ~
If we pushed another directory – “d3”, stack will look like d3 d2 d1 ~
Now suppose you wish to switch directory say d2, then use below command.
You can think of stack as an array with first value accessed at 0. Now we will be switched to “d2” and stack will look like d2 d1 ~ d3
To change to directory “d3”, you can use below command.
“popd” command is used to remove the directory from stack.