This article talks about the basic file operations commands in Linux.
For more on this subject, please refer to the SOC Table of Contents.
Creating a new file using touch
Compared to the previously discussed VIM, touch is a more comprehensive command for creating new files. The formatting is as follows:
touch [option] [file name]
When using touch to create a new file, if the specified file name doesn’t exist, it will directly create a file with that file name; if the file name already exists, it would simply change the file’s date last modified.
The most common options are as follows:
-a Only change the date last modified
-c Doesn’t create any file
-d<date> Uses the specified date instead of the current date
-t<time> Uses the specified time instead of the current time
When under the user’s root directory, one can directly use the command “cd ~” to quickly access the user’s root directory. We can then use the touch command to create a file named “test”, as shown below.
touch test creates the test file, while ls test allows us to view the test file we just created.
Finally, ls test -l allows us to view the details of the test file that we just created.
Creating folders using mkdir
The formatting for creating folders using mkdir is as follows:
mkdir [option] [Folder-Name Directory-Name]
The most common option is
-p If the parent directory of the directory you wish to create hasn’t been created yet, it will be created.
Here we will create two folders under the user’s root directory as an example. For convenience, we will name the folders testfolder1 and testfolder2.
By using the ls command once more, we can see that we have successfully created the two folders.
Deleting folders and directories using rm
Using the rm command can delete not just one file at once, but it can also delete multiple at once. It can also achieve recursive deletion. Regarding shortcut files, it would only delete the shortcut and not the original that is associated with it. The formatting is as follows.
rm [option] [target directory of the file or folder you wish to delete]
The main options are as follows:
-d Directly changes the hardwired data of the file at the directory you wish to delete to 0, deleting the directory.
-f Force deletes the file and the folder (directory)
-i Asks the user for confirmation before deleting the file and the folder (directory)
-r Recursively deletes all of the files and folders under the directory.
-v Display the entire deletion process.
As an example, we will delete the test file that we created using the touch command.
We first display all of the files using the ls command.
Next, we use rm test to delete the test file.
Finally, we use ls once more to verify that the file has been deleted.
Now, we will also demonstrate deleting a folder by deleting testfolder1.
You may find that directly using rm on the folder can’t delete it.
Note that we need to use option r as well as f in order to delete it. We may combine the two when writing it, so it would just be -rf.
Deleting the folder directory using rmdir
Note how we had to use the option -rf to delete the folder above.
We are fortunate enough that Linux provides a command to directly delete a folder directory, which follows the following format.
rmdir [option] [folder (directory)]
It has the main option
-p Deletes the specified folder (directory), and also deletes the parent folder if it is empty.
Now we will use rmdir to delete the previously created testfolder2.
Copying files using cp
We know that copy and pasting in the graphical interface is fairly straightforward. But in order to achieve the same in the Terminal, we would need to use the cp command. The formatting is as follows.
cp [option] [original location] [target location]
The main options are as follows.
-a Shortcut for the -dpR option
-d When copying files with a symbolic link, keep the original link
-f Force copies the file, without caring if the copied file already exists in the target location.
-I Ask the user before replacing an already-existing file
-p Keep the properties of the original file or directory
-r or -R Recursively processes the specified location as well as everything under the specified location
We will first create two folders using the mkdir command. For convenience, we will name them test1 and test2.
Next, we will go into the test1 folder that we just created, and create a file named a.c inside. We then use ls to verify if the action was successfully completed.
We will copy the file into the test1 folder, and name the copied file as b.c.
Next, we will copy both a.c and b.c to the folder test2. Using replacing the first part of the file names with “*”, like so: “*.c”, allows for it to select all of the files in test1 that end with “.c”.
Notice how we used “../” to take us to the parent directory which is where the two folders are located, as we were still inside the test1 folder.
We will now copy the entirety of the folder test2 to a new folder named test3.
Moving files using mv
Sometimes we may need to move a certain file or folder to another location, or we may want to rename a file or folder. Thus, we can use the mv command with the following format:
mv [option] [original address] [target address]
The main options are as follows:
-b Back up the file first if a file would be overwritten or replaced.
-f If the target location or directory is the same as an existing file, directly overwrite the file that is there.
-I Ask the user before overwriting anything.
As an example, we will first create a file titled “c.c” inside the test1 folder that we previously created, and then rename the file as “d.c”.
Now, we will move the “d.c” file to the test2 folder.