Getting started on the Linux command line might seem overwhelming at first, but the many commands you need to use will fall into place more quickly than you might imagine. Let’s start with some introductory but very important commands.
First, when you open a command terminal or log into a remote Linux server, you will be sitting at the command prompt. This might be just a $ or might be something as complex as [george@system ~]$ that will change as you move from one directory to another. You, however, will be located in your home directory. The first commands you will need to know include pwd and ls.
1. pwd command: display the current directory (e.g., /home/george)
2. ls command: list the files in your current location
You’ll likely not see any files listed when you first try ls. That doesn’t mean there aren’t any files. It just means that you’ll have to work a little harder to see them. The ls command assumes that you don’t want to see files that start with dots and any files that are included in a new home directory, like .bashrc, start with dots. Use the ls -a command and, voila, you’ll see some files.
3. cat command: display file contents of a text file
To view the content of a .bashrc file or any text file for that matter, you can use the cat command. No, this has nothing to do with our feline friends. This cat stands for “concatenate”, but that’s a story for some other chapter of your Linux voyage.
4. more command: display a text file one screenful at a time
5. less command: display a text file one screenful at a time and allow backing up
If you’re trying to display a text file that has lots of lines, the cat command will display the file as the lines rapidly move up your terminal window. If you want to view these files in a screenful-at-a-time manner, use the more or the less command. These commands are very similar, but less lets you back up if you want using the up arrow key.
6. cd command: move into a particular directory or back to your home
To move to another directory, use the cd command. For example, you can move into the /tmp directory with the command cd /tmp. Moving back to your home directory is even easier. Just type cd by itself and you’ll move right back to your home directory.
7. touch command: create blank/empty files
To create a new file, you can use the touch command. This command (e.g., touch newfile) creates an empty file but, hey, you’ll have more files to list.
8. echo command: display the specified text
If you want to add a little content to your new file, you can use the echo command and redirect the output into a file with a command like echo “read me” > newfile. If you run that same command again, the file will not change. That’s because the single > in the command will cause the echo command to overwrite any prior content. If you want to add a line to an existing file, use >> instead.
9. rm command: delete a file
If you want to delete a file, you would use the rm command (e.g., rm myfile). As you might suspect, you can only remove files that you own. So, this is a good point to try another ls command – ls -l. This command will not just list your files, but will display them with lots of extra details as in this example:
-rw-r–r–. 1 george george 8 Nov 6 13:28 newfile
“What’s all that about?” you ask. The initial – means that we’re looking at a regular file. In other words, it’s not a directory or a file that points to another file. The rw- part means that you (george in this example) have read and write access to the file. If anyone else is a member of the george group (yes, the group would exist), they would have read access only (the following r–). Note that it is almost never the case for user groups like george to have more than a single member. The final r– would give read permission to anyone else who logs into the system if they had access to your home directory which is almost never the case. In general, only the owner of an account and the all powerful root account can mess with your files.
10. mv command: rename a file
To rename a file, use the mv command (e.g., mv newfile oldfile). Note that, if you include a full path, you can not just rename a file, but you can move it to some other location provided you have write permission to that directory.
To move your file into the /tmp directory, which anyone can use, use a command like this: mv newfile /tmp
If you want to move a file and rename it at the same time, you can use a command like this: mv newfile /tmp/oldfile
11. cp command: copy a file
To copy a file, use the cp command (e.g., cp thisfile thatfile). You can copy a file to another directory by using a full path (e.g., cp myfile /tmp/yourfile).
12. passwd command: change your password
If you need to change your password, use the passwd command. You’ll be prompted to enter your current password and then to enter the new password twice. Just don’t be surprised if it doesn’t work the first time. Linux systems tend to be fussy about passwords. They generally want them to be so many letters long and not based on common words. Make sure that you’ll be able to remember the password you end up choosing or you’ll likely need the assistance of someone with access to that powerful root account to reset it for you.
Wrap-up
Well, that’s a start. If you’re intrigued by this introduction to the Linux command line, please be on the lookout for the next installment.