The rmdir command is used to remove empty directories. (Barrett, 2016) Its usage is as follows:
rmdir emptyDir1
or (for deleting multiple empty directories):
rmdir emptyDir1 emptyDir2 …
Note that rmdir only removes empty directories. To remove directories that contain files, use rm -r, but please do remember to use it with extreme caution!
Thank you for reading and hope you learned something useful!
References
Barrett, D. J. (2016). Linux pocket guide (3rd ed.). O’Reilly Media. Page 55
The touch command is used for changing file timestamps (or creating new files). (“touch(1) – Linux man page,” n.d.) It is used as follows:
touch fileName
If the file named fileName does not exist, the file with the name fileName is created. If the file named fileName already exists, then only its modification time is modified to the current time.
I have used the touch command a few times to create new files.
In order to emphasize what can happen if you are not careful while using the rm command, I will tell you a story where I used rm the wrong way.
I was mangling with some drivers. Everything was going fine. I was deleting some files, adding some new files. Nothing fancy. However, at one point, I was a little bit distracted and pressed Enter while I had an instance of the rm command in my Terminal. With that, I just deleted some very important graphics driver files. Ooops!
Obviously, I tried to bring it back, but if you Google something like “how to restore files deleted with rm” you will find that while there are ways, they are all pretty complicated and involve a lot of hassle. That’s what happens when you aren’t careful. I had to reinstall my operating system to fix the issue caused by one accidental rm.
So, once again: Be careful with rm. rm does not forgive.
The rm command is used to delete files and directories. (Shotts, 2019) It is used as follows:
rm item1 …
where item1 can be either a file or a directory, and … signifies you can specify multiple files or directories.
Two options I found very useful with rm: -i is used to prevent rm from silently deleting files by prompting you before actually deleting them and -r is used for recursive deletion (meaning that both a directory and its subdirectories will be removed).
Be very careful with rm! Let me say that again: Be very careful with rm!rm does not forgive! I highly recommend checking out (“Where do files go when the rm command is issued?,” n.d.)
Hope you don’t accidentally delete some files with rm and hope you found this useful!
The mv command is used to move or rename files or directories, depending on its usage. (Shotts, 2019) To rename or move files or directories, use it like this:
mv oldFileName newFileName
If you want to move one or more files from the source directory to the destination directory, use:
mv file1 file2 … desination
A couple of remarks: Use the -i option to prevent the mv command from silently overwriting files (files are overwritten when they have the same name in both the source and the destination) and use the -u option only move files that either don’t exist or are newer than the corresponding files in the destination directory.
The cp command copies files or directories. (Shotts, 2019) It can be used to copy only one file or directory to a destination or multiple files or directories to a destination. I remember the syntax most easily by remembering the following:
cp source destination
So first you specify what you want to copy, then where you are copying it to. For multiple files, I remember it by:
cp source1 source2 … destination
Let’s say I have the following situation:
mislav@mislavovo-racunalo:~/grep-hadoop-example$ ls
part-r-00000 _SUCCESS
and say I want to copy the file part-r-00000 to /home/mislav directory. I would do that by writing:
Now when I position myself in my /home/mislav folder, I see the file I just copied:
mislav@mislavovo-racunalo:~/grep-hadoop-example$ cd ..
mislav@mislavovo-racunalo:~$ ls
anaconda3 hadoop-example snap
'Calibre Library' Music stanfordnlp_resources
Desktop part-r-00000 Templates
Documents Pictures Untitled.ipynb
Downloads Public Videos
grep-hadoop-example Python-3.7.4
Great! Two more useful things:
You can run cp with the -i option, which asks you if you want to override a file. If I were to copy another file named part-r-00000 to /home/mislav, cp would silently overwrite the existing file. You would never know! However, if you run it with the -i option, it doesn’t silently overwrite files.
Another useful option is the -u option, which only copies the files that either don’t exist or are newer at the source than at the destination.
Alright, I am leaving you here. Hope you learned something useful.
Let’s talk about how you can use auto-completion in the shell. When typing a filename, press Tab on your keyboard.
Let’s say I have a file named longFileName.txt. I can just type in long, press Tab and the filename will be filled for me automatically. This does not hold if I have two or more files starting with long. There must be only 1 file with that prefix in its filename (any ambiguity must be cleared) for auto-completion to work.
A useful option of the ls command is -l. The -l option is the “long listing format”, which lists the following (from the first to the last column):
file permissions,
number of links,
owner name,
owner group,
file size,
time of last modification, and
file/directory name (“What do the fields in ls -al output mean?,” n.d.)
I use the ls -l option to find out the size of the files within a directory. My main concern is if the files are empty (0 bytes) or if they are larger. This is because I write programs and if a program is actually writing something to a file, the files should be larger than 0 bytes.
An example output of the ls -l command is:
mislav@mislavovo-racunalo:~/grep-hadoop-example$ ls -l
total 4
-rw-r--r-- 1 mislav mislav 22 Nov 9 18:32 part-r-00000
-rw-r--r-- 1 mislav mislav 0 Nov 9 18:32 _SUCCESS
The option I use less often, but is mentioned in the above reference is the -a option. The -a option flag lists all files, even the hidden files (hidden files are specific in that their filename starts with a .). I never had the need to look at hidden files as those are usually reserved for some operating system things or some program configuration things, but if you want to explore, off you go with ls -a! You can combine both the l option and the a option by writing ls -la. You can usually combine options like that.
If you don’t want to read through man pages of a command to find out how it is used, you can specify the --help flag (its equivalent short option is -h) when executing a command, which will give you a description of how the command is used in a very concise and clear way.
Not all (executable) commands support the --help flag, but I found myself using --help more often than man.
Thank you for reading and hope you learned something useful!