The cat command is used to view contents of a file. More specifically, the cat command reads the contents of one or more files and copies them to standard output. (Shotts, 2019)
This was a short text file, but for large files, output of cat is messy. There are other commands (such as less) which also show you the contents of a file, but in a much more convenient fashion. We will talk about these other commands very soon.
The echo command is used to print its arguments to standard output. (Ward, 2014) “Standard output” is, in most cases, another word for “your monitor”.
It is used like this:
echo somethingToEcho
For example:
mislav@mislavovo-racunalo:~$ echo Wondering my usefulness, reader? Well, I will be useful later on, for now, just remember that I exist!
Wondering my usefulness, reader? Well, I will be useful later on, for now, just remember that I exist!
Oh my God, the command can talk! It is actually accurate in what it said – it is useful, but later when you learn about things such as expansions of shell globs. But that’s for another post.
Hope you learned something useful!
References
Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Page 16
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.