In this writing we are going to go over basic commands inside the linux terminal. This will hopefully help to familiarize you with the command line and hopefully show you some tricks that will be useful in your linux endeavors.
Let’s first learn about some very useful tools that will help you learn about what each command is. These commands are whatis, man, and help.
whatis
whatis will give a short description of what a command does, while man will give you a compete rundown of the command plus what options can be used with it.
[jeff@vulnifo ~]$ whatis ls
ls (1) - list directory contents
ls (1p) - list directory contents
[jeff@vulnifo ~]$ whatis cd
cd (1) - bash built-in commands, see bash(1)
cd (1p) - change the working directory
As you can see whatis provides a brief and to the point description of what the commands do.
man
Now lets take a look at the output from man.
[jeff@vulnifo ~]$ man ls
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is speci‐
fied.
Mandatory arguments to long options are mandatory for short options
too.
-a, --all
do not ignore entries starting with .
-A, --almost-all
do not list implied . and ..
--author
...
[jeff@vulnifo ~]$ man whatis
WHATIS(1) Manual pager utils WHATIS(1)
NAME
whatis - display one-line manual page descriptions
SYNOPSIS
whatis [-dlv?V] [-r|-w] [-s list] [-m system[,...]] [-M path] [-L
locale] [-C file] name ...
DESCRIPTION
Each manual page has a short description available within it. whatis
searches the manual page names and displays the manual page descrip‐
tions of any name matched.
name may contain wildcards (-w) or be a regular expression (-r). Using
these options, it may be necessary to quote the name or escape (\) the
special characters to stop the shell from interpreting them.
index databases are used during the search, and are updated by the
mandb program. Depending on your installation, this may be run by a
periodic cron job, or may need to be run manually after new manual
pages have been installed. To produce an old style text whatis data‐
base from the relative index database, issue the command:
...
man is very detailed and offers everything you need to know about the command you searched for.
For a nice middle ground between whatis and man, we can also use the help command.
help
[jeff@vulnifo ~]$ help cd
cd: cd [-L|[-P [-e]] [-@]] [dir]
Change the shell working directory.
Change the current directory to DIR. The default DIR is the value of the
HOME shell variable.
The variable CDPATH defines the search path for the directory containing
DIR. Alternative directory names in CDPATH are separated by a colon (:).
A null directory name is the same as the current directory. If DIR begins
with a slash (/), then CDPATH is not used.
If the directory is not found, and the shell option `cdable_vars' is set,
the word is assumed to be a variable name. If that variable has a value,
its value is used for DIR.
Options:
-L force symbolic links to be followed: resolve symbolic
links in DIR after processing instances of `..'
-P use the physical directory structure without following
symbolic links: resolve symbolic links in DIR before
processing instances of `..'
-e if the -P option is supplied, and the current working
directory cannot be determined successfully, exit with
a non-zero status
-@ on systems that support it, present a file with extended
attributes as a directory containing the file attributes
The default is to follow symbolic links, as if `-L' were specified.
`..' is processed by removing the immediately previous pathname component
back to a slash or the beginning of DIR.
Exit Status:
Returns 0 if the directory is changed, and if $PWD is set successfully when
-P is used; non-zero otherwise.
As you can see, each command gives a description to the command in a different way, as well as different options and what they do for each command. These are a VERY useful tool that can help you along your journey.
Now that we know what cd and ls do, lets use them.
cd
cd (change directory) is to navigate through the file structure. You can find where you are in the filesystem by typing pwd.
[jeff@vulnifo ~]$ pwd
/home/jeff
Now lets say I want to change to my documents folder which is inside my home folder. Since I am currently inside /home/jeff I need not enter this in that path. I will show both ways, to show they reveal the same output.
[jeff@vulnifo ~]$ cd /home/jeff/Documents
[jeff@vulnifo Documents]$
[jeff@vulnifo ~]$ cd Documents/
[jeff@vulnifo Documents]$
As you can see, the shell tells us what folder we are in once we change directory to it.
ls
While I am in my Documents folder, I would like to be able to see what is inside of it. To do this, we use the ls (list directory) command. There are many options you can use to modify this command that can be found in the man pages, I will go over a few different ones.
[jeff@vulnifo Documents]$ ls
[jeff@vulnifo Documents]$
It looks like my documents folder is empty! Lets add a file using the touch command.
touch
The touch command allows you to create a new empty file. Let’s see it in action by creating the file and then using ls to see it.
[jeff@vulnifo Documents]$ touch mynewfile
[jeff@vulnifo Documents]$ ls
mynewfile
The ls command has some useful modifiers, we will use the -l option and view more information.
[jeff@vulnifo Documents]$ ls -l
total 0
-rw-rw-r--. 1 jeff jeff 0 Jun 28 13:48 mynewfile
Whoa there is a lot more information on there. Lets break it down.
-rw-rw-r–
This is your permissions section. It is broken down into 3 parts, which are permissions for owner, group, all.
The very first “-” denotes wether it is a directory or not, signified by a “d” in this place. Proceeding sections are broken into parts of 3, for each user. R stands for read permission, W stands for write permission, and an E signifies an execute permission. These can all be changed with the chmod command but that is out of scope for this tutorial.
The next section “jeff jeff” is telling you both the Owner and the Group that this file belongs too. These can be changed with the chown command, but again it is out of scope for this tutorial.
The next section is the date and time the file was last modified, and finally; the name of the file.
mkdir
Let’s create a new directory in our Documents folder by using the mkdir command.
[jeff@vulnifo Documents]$ mkdir newdirectory
[jeff@vulnifo Documents]$ ls -l
total 0
-rw-rw-r--. 1 jeff jeff 0 Jun 28 13:48 mynewfile
drwxrwxr-x. 2 jeff jeff 6 Jun 28 14:01 newdirectory
There we used ls again and we can see the new directory we just created (notice the “d” in the permissions section!)
cp
Now we want to copy mynewfile into our newdirectory. For this we use the cp command.
[jeff@vulnifo Documents]$ cp mynewfile newdirectory/
In this command we use cp, the file we are copying, and where it is going. (Again we were already in the Documents folder so we did not need to include the full path. But for clarification, the command could also be – cp mynewfile /home/jeff/Documents/newdirectory
Now lets cd into the directory and ls to see what’s in the directory.
[jeff@vulnifo Documents]$ cd newdirectory/
[jeff@vulnifo newdirectory]$ ls
mynewfile
And there it is!
Lets backup one directory by using cd. Using two “..” after the cd command will take you to the previous directory.
[jeff@vulnifo newdirectory]$ cd ..
[jeff@vulnifo Documents]$
mv
Now we are going to use the mv (move) command to move our file to another directory while also changing its name. Since we are in documents, and we just copied mynewfile into newdirectory. We are going to use mv, to move mynewfile into the newdirectory and change its name to mynewfile2.
[jeff@vulnifo Documents]$ ls
mynewfile newdirectory
[jeff@vulnifo Documents]$ mv mynewfile newdirectory/mynewfile2
[jeff@vulnifo Documents]$ ls
newdirectory
Now you notice while using the mv command that the file is no longer where it originally was, rather than copying it it moved it to the new location. Lets cd to the new location and take a look at what’s in there.
[jeff@vulnifo Documents]$ cd newdirectory/
[jeff@vulnifo newdirectory]$ ls
mynewfile mynewfile2
And there you have it, we now have both files in the newdirectory.
echo
The echo command is used for displaying a line of text. If we just use it in the terminal, we can display something in the terminal.
[jeff@vulnifo newdirectory]$ echo Im Learning Linux!
Im Learning Linux!
Now lets use echo to add a line of text to one of our files.
[jeff@vplex newdirectory]$ echo "This is a new line of text!" >> mynewfile
This is a more advanced usage of the echo command, we must quote what we want to say, and then use the “>>” to tell it where to add the content.
cat
Since we just added a line of text into our file, we can now view that file using the cat command.
[jeff@vulnifo newdirectory]$ cat mynewfile
This is a new line of text!
[jeff@vulnifo newdirectory]$ cat mynewfile2
[jeff@vulnifo newdirectory]$
You can see the line of text we added to the first file, and that the second file is still blank.
rm
Now that we know how to create files, directories, and add content to them, let’s now delete one of our files.
mynewfile2 doesn’t have any content in it, so we might as well delete it. We use the rm command to do this.
[jeff@vulnifo newdirectory]$ ls
mynewfile mynewfile2
[jeff@vulnifo newdirectory]$ rm mynewfile2
[jeff@vulnifo newdirectory]$ ls
mynewfile
[jeff@vulnifo newdirectory]$
And there you have it, the file is gone!
Conclusion
This concludes our basic linux command line tutorial. Hopefully you found this information useful. Remember to share if you think this tutorial can help someone you know, and also like/follow our social media to keep updated with what we are doing!
Follow us on twitter at @vulnifo to stay updated with new posts and information!
Visit our contact page for more ways to get in touch.