In this post, we will learn some commonly used file operations when working with Linux operating system. We can use these commands in the bash terminal or in bash scripts.
Creating a Directory
mkdir command is used to create a directory in Linux. To create a directory called ubuntu, type the following command in the terminal window:
$ mkdir ubuntu
The new directory would be created under the present working directory. To navigate we can use the cd command.
Removing a Directory
rmdir command is used for removing the directory. Common errors while using this command is:
providing a non-existent directory name.
trying to remove a directory which is not empty.
:~/ubuntu$ rmdir ubuntu
rmdir: failed to remove ‘ubuntu’: No such file or directory
:~/ubuntu$ touch sample.txt
:~/ubuntu$ cd ..
:~$ rmdir ubuntu
rmdir: failed to remove ‘ubuntu’: Directory not empty
Creating a File
There are so many ways to create a file in Linux. Using touch command, cat command, etc.
touch command creates a new file if it doesn’t exist in the directory. If the file already exists then it will change the modification timestamp of the file. To learn more about touch command use
$ man touch
To create a new file with touch, use the following command:
$ touch sample.txt
Copying a File
cp command is used in Linux to copy the file. This command copies the file to the new location.
Format of the command is
cp file <new location>
$ cp toBecopied.txt ubuntu/
Moving a File
mv command can be used to move the file to a new location.
Removing a File
We can use the rm command to remove a file.
$ rm -rf sample.txt
To know the flags and to learn more about the command, use the below command: