When using Linux, half — or more — of the fun is opening up a terminal, leaving behind the GUI, and immersing yourself in the keyboard.
What this requires is little more than the knowledge of a few basic commands, largely related to navigation and file system manipulation.
Once you use each command, and become more comfortable with each one individually and in combination, it is a simple process to add more commands into the mix.
Just remember that in Linux, a file name is not complete unless it includes the file extension.
And although I rarely specify, none of these commands will work unless you press return (hit Enter) after typing them in.
man
Definition: Manual
Let’s begin by getting our priorities straight and admitting that sometimes we need a little help figuring things out.
In the context of the Linux command line, the man command is that help, as it provides reference manuals for every available command.
To learn about any command (what it does, its syntax, what arguments are available, etc.) type in man command
(where the command is replaced by the command in question).
You can use the up/down arrow keys, the up/down page keys, or the middle mouse wheel, to scroll through the “man page”. To exit, hit the q key on the keyboard.
To learn more about the man command, look it up: man man
As an alternative, you can type the –help option after any command for that command’s details to be printed. Example: man --help
pwd
Definition: Print Working Directory
Your location, when you first open the terminal, will be your own home directory. Similarly, if you switch to another user name from the terminal, you will be deposited in that user’s home directory.
But, after moving around a bit in the file system, or when coming back to the terminal after an absence, it can be difficult to remember just exactly where in your file system you are — much less how or why you got there!
I can’t help with the why you got there, but I can help with the where that you are. Or rather, I can help you learn what command to use to find that out.
Type in the pwd
command and hit Enter. This command will “print working directory”. The long-winded explanation is that it will display the directory (and complete path to the directory) that you are currently in.
It’s very handy!
cd
Definition: Change Directory
cd is the one command that I use the most — hands down. It’s the terminal substitute for all of the clicking that we do in the GUI, that moves us around the file system, into directories and back out of them.
Following the command itself, you will need to specify where to go, or which directory to change to. If you don’t you will always be taken back to your home directory. This is where it starts looking a little tricky, but it’s deceptively simple.
A path is an established route or course designed to be traveled on. In the Linux file system there are two paths: relative and absolute.
A relative path is one directly related, or relative, to the directory you are already in. For example, if you know you are in your home directory, then cd Downloads/
expresses a relative path that will take you into the downloads directory in your home directory.
An absolute path is one directly related to the root directory of your file system. For example, regardless of where you already are in your file system, typing in cd /home/gwen/Downloads
expresses an absolute path directly into the specified user’s downloads directory. The starting / (remember, this refers to the root directory) is what indicates an absolute path.
So with those basics in mind, let’s look at a few shortcuts that make the cd command even easier to use.
The use of a . is the equivalent of specifying your current directory, so from your home directory, cd ./Downloads/
is the same as saying cd Downloads/. Keep this one in mind for later, as it is very useful for other commands!
The use of two periods .. is used in reference to the parent directory (up/back one from the current directory.) It can be used, for example, when in the downloads directory to return to the home directory, like cd ../
. Multiple instances can be used at once to backtrack multiple directories at once: cd ../../
Use cd ~
to return to the current user’s home directory at any time.
Use cd -
to return to the last directory that you were in.
One more thought that is important to note: You can move to, or access, any directory/file in your current directory without specifying it’s path, but to move to another directory or access a file in another directory without first changing to that directory, you will need to specify it’s path either absolutely, or relative to your current location.
ls
Definition: List
The ls command will list the contents of a directory.
Used without options/arguments, it will list the current directory contents without details, in alphabetical order.
Used with the -a argument, it will show all of the current directory contents, including hidden files and directories.
Used with the -l argument, it will show the current directory contents in a long list format that shows additional details.
Bonus: The ll
command is a shortcut for ls -l
and it’s super-simple to type in!
Multiple arguments can be grouped together. Instead of using -a and -l separately, you can combine them: ls -al
You can print the contents of a directory that you are not currently in, by specifying the path to the directory in question. Example: ls -al /home
cp
Definition: Copy
To create a copy of a file or directory, you can use the cp command.
Two arguments are required by this command. They are source and destination, in that order. It helps me to remember the order if I keep in mind the phrase “copy what to where”.
The cp command can be used as simply as making a copy of a file in your current directory. Example: cp filename.txt CopyOfFile.txt
The cp command can be used to make a copy of a file in a different directory. Example: cp filename.txt /temp/backup/CopyOfFile.txt
The cp command can be used on directories, but requires the -r or -R “recursive” argument be included. Example: cp -r /home/gwen/dirname/ /temp/backup/
A symbolic link, or soft link, (basically a shortcut to a file) can be made by using the cp command’s -s argument, but the file names have to both specify the absolute path. Example: cp -s /home/gwen/file.txt /home/gwen/linktofile.txt
Multiple files can be copied by specifying multiple file names (separated by spaces) and a single destination (directory, not file.) Example: cp One.jpg two.jpg three.jpg /temp/backup/
mv
Definition: Move
The mv command is used to move a file or directory to a new location/directory.
It is also be used to rename files or directories by “moving” them to a new name.
As with cp, the mv command syntax is as simple as keeping in mind the phrase “move what to where”. Example: mv file.txt /temp/
For renaming purposes, simply specify the complete source and destination file names (along with the paths, if the file is or will also be moved to a directory outside of the current one.) Examples: mv file.txt newfilename.txt
or mv file.txt /temp/newfilename.txt
Multiple files can be moved by specifying multiple file names (separated by spaces) and a single destination (directory). Example: mv file1.txt file2.doc file3.jpg /temp/backup/
To move or rename an entire directory, simply specify what to where (source an destination) without additional arguments. Example: mv testdir/ /temp/backup/newtestdir
touch and stat
Definition: Touch and Status
A timestamp is the data recorded in a file that tells when (what date and time) the file was last accessed, modified and/or changed.
To view the timestamp (and other file data), type stat filename.txt
. (Replace “filename.txt” with the full name of the file in question.)
To update the timestamp to the current date, type touch filename.txt
. (Replace “filename.txt” with the full name of the file in question.)
Use the –date= option with the touch command, to update the timestamp to a specified date. Example: touch --date=07/11/2015 filename.txt
The touch command is a good way to create new files, as it will create a file if it cannot find the file name specified.
mkdir
Definition: Make Directory
The mkdir command will create a brand new directory (as long as the specified directory does not already exist).
The syntax is simple. Just type the command, followed by the directory name, to create it in the current directory, or the path and directory name, to create it in another directory. Examples: mkdir testdir
or mkdir /home/gwen/testdir
rm and rmdir
Definition: Remove and Remove Directory
Today’s two-for-one special is a dangerous combination of commands that allow you to delete files and directories.
You can, in fact, delete your entire file system if you’re not careful. Not to scare you, or anything. And don’t worry, even in my inexperience, I’ve never personally done that. I do know someone who has — at least twice — accidentally wiped out his entire file system and had to re-install Linux. Live and learn!
The rm command will remove a specified file or specified files instantly, bypassing the Trash bin and any other forms of recovery completely.
The rmdir command will remove a specified directory if it is already empty.
To delete a full directory, use rm -r and specify the directory name (and path, if necessary).
Examples: rm file.txt
, rmdir testdir
and rm -r testdir
head
The head
command will print (display) the first 10 lines of a specified file or files.
The options (called arguments) allow for the number of lines to be specified, if you need to see more/less than the default 10.
tail
The tail
command will print (display) the last 10 lines of a specified file or files.
The options (called arguments) allow for the number of lines to be specified, if you need to see more/less than the default 10.
exit
Typing in exit
will close the terminal.
Alternatively, the keyboard shortcuts Ctrl + Shift + W or Ctrl + Shift + Q will work too.
Conclusion
I can hear you already, saying, “Wait! That was more than 12 commands!”
Yes, but there are just too many good ones to resist throwing in a few extras.
For the record, I can count!