Linux Training : 2. basic everyday commands

Basic everyday commands

[ Basic everyday commands ] [ ls ] [ echo ] [ cat ] [ cp ] [ mv ] [ mkdir ] [ cd ] [ tar ] [ touch ] [ cut ] [ history ] [ find ] [ xargs ] [ Table of additional commands ] [ Basic commands exercise ]


The workhorse of most users. ls is used to show the contents of a directory. It has many flags that modify it's output. Some particularly useful ones are

  • ls -l long listing with timestamps (will show the real source for symlinks)
  • ls -la long listing with all hidden files and hidden directories that start with a "."
  • ls -rt list files in reverse time order showing most recent files at the end. Makes finding that new one easy.
  • ls -1 (that's the number one) lists the directory contents in a single column. Good for scripting.
  • ls -Z show the SELinux context for a directory or file.
try ls

Look at the difference between ls and ls -la in your home directory.

Those listings that start with a . (dot) are "hidden". They are most often used as configuration for user level applications

Try running ls -lart and see how the files are rearranged.
Lastly, run ls -Z /tmp. The section that has text separated by : are called SELinux file contexts.


echo will take the contents of a variable and output it to STDOUT. Use this to find out what's in a variable echo $HOME where $HOME is a user environment variable (see your user environment with the env command.).

try echo

Use echo to quickly create text files with echo "Hello, World" > foo.
Also, you can look at the absolute path to your home directory with echo $HOME


cat is short for concatenate. It is used to join 2 or more files in sequence and display the contents to the screen (STDOUT).

cat /etc/filesystems /etc/shells

will display /etc/filesystems followed immediately by /etc/shells. By using LIN:IO redirection the contents can be sent to a file or into a string of other processes.

cat /etc/shells /etc/filesystems > /tmp/catfile

will create a file /tmp/catfile and put /etc/shells and /etc/filesystems into it.

cat shells | grep "sbin"

will take the output of the cat and use it as the input for the grep searching for "sbin". This is an overly complicated process as grep "sbin" /etc/shells is more streamlined and appropriate but it has the same effect.

try cat

Type the command cat /etc/filesystems - >> /tmp/foo. When you get the blank line, start entering text. A new line is given when you press "Enter". To stop entering text press "Ctrl-d" ( "Ctrl-d " is a EOF, or End Of File ). You should have a file /tmp/foo that has the contents of the /etc/filesystems followed by the lines you entered. The secret to the lines you entered was the -. That is a shortcut that means "get the data from the keyboard"
To read your file, type cat /tmp/foo


cp will create a copy of a file or files. It have a multitude of flags that also allow keeping intact the file permissions, timestamps and many other things.

  • cp -p will preserve mode, ownership and timestamps
  • cp -i will prompt for permission to overwrite a file of the same name.
  • cp -r will do a recursive copy for an entire directory and all it's subdirectories.
  • cp -a will do a recursive, archival copy keeping all ownerships, timestamps, symlinks data, everything. It's a poorman's backup command.

    Only root can keep the ownership of files intact when using *cp -a. It would be a big security problem is an existing shell script, owned by root could be copied by a normal user and kept with root ownership.

    cp -a /etc/filesystems ~. Look at it with ls -l and cat. Now try and copy it again to the same location but use the -i flag instead of the *-a flag. You will have to enter a "y" to actually copy it. Again look at the file with ls -l and note what changed.


This is used to relocate a file from one place to another as well as to rename a file.

mv /tmp/foo ~

will move the file foo from /tmp to the users home directory. The original file /tmp/foo is no longer in /tmp.

mv foo.txt foo.bak

will change the name of the file foo.txt to foo.bak. The mv command requires the user have rw permissions on both end of the process.

try mv

Create a file called file1 with the command:

touch file1

Rename file1 to myfile.txt
Now move that file to /tmp
Lastly, move the file from /tmp back to your home directory while changing its name to foo.txt


mkdir is used to make directories:

mkdir dir/

Using mkdir with -p will create all parent directories if they do not exist. Without -p, creating a directory inside a non-existing directory will fail.

mkdir -p dir1/dir2/dir3/
The opposite of mkdir is rmdir
try mkdir

mkdir /tmp/test and ls /tmp/test to verify
Now mkdir /tmp/test/foo/bar and notice the failure message.
Make it work with the -p flag and verify with ls
Remove the directory /tmp/test.


Use this to change directories. Either enter the directory with a full path

cd /etc/sysconfig/network-scripts

or use the dot notation for directories above

cd ../../etc/yum.repos.d

where the ".." means "go up one level". A single "." means "right here in this directory".

  • cd [LIN:enter] will jump the user's home directory as will cd (The ~ is a shortcut for the users home directory).
  • cd - will go back the previous directory the user was in (This is stored in a variable called OLDPWD. Type echo $OLDPWD to see it)
try cd

cd /tmp and look around with ls
Now cd / and look around.
Use the shortcut to go back to the /tmp directory.
Use the shortcut to go back to your home directory.

Where am I?

pwd will show you the present working directory.


tape archive has more flags than can be covered in a week. It was originally used for copying data to and from tape drives (it is still used for that). It is similar to zip in that it can take a bunch of files and make a single archive file and compress it. The file created by the tar command is often called a "tarball".

  • tar cvzf file.tar.gz foo/ bar/ baz* will create (c) a new archive file (f) called file.tar.gz while showing what is being done verbosely (v) and compressing with zip (z) sourcing from the directories foo/ and bar/ and any file beginning with baz in the current directory. This is a major workhorse function along with it's untar sister:
  • tar xvzf file.tar.gz will uncompress (z) the file (f) file.tar.gz being verbose (v) and extract (x) to the current directory the contents of the archive.
  • tar tvzf file.tar.gz will show the contents of a gzipped tarball (useful to use before the tar xvzf to make sure the archive has it's own directory and doesn't just plop a ton of files in the current directory!)
  • tar xvjf file.tar.bz2 will unpackage a tarball compressed with bzip2 compression. Note the changed flag and the archive ending.
  • tar xvjf file.tar.gz -C newdir will untar file.tar.gz after changing directories into newdir.
  • tar xvjf file.tar.bz2 foo.txt will extract only the file foo.txt from the tarball.
try tar

Create a tarball of the /etc directory in the /tmp directory.
Extract only the /etc/sysconfig folder from the tarball into the new directory /tmp/newdir


this will create an empty file with the given filename or update the timestamp on an existing file.

try touch

touch /tmp/testfile and read the timestamp with ls.
Use echo to add some text to the file and recheck the timestamp.

The timestamp you see is the "last modified" time (mtime). There are other times tracked by the system such as creation time (ctime) and access time (atime). These can be seen with ls -l --time=STYLE where STYLE is ctime or atime.

touch /tmp/testfile again and read the timestamp with ls.
cat /tmp/testfileand check the atime timestamp.


  • cut -f 3 -d ":" </etc/passwd will output a list of UID's. Cut will extract fields from a string or lines from a file using a defined single character delimiter. If -d is not specified, the default delimiter is the [LIN:tab] character.
try cut

Extract a list of group names and GIDs from /etc/group


The history command will show the last (bazillion but closer to 1000) commands from the current session. This is an immensely useful tool. It's a built in part of most shells and bash history is quite good. The storage place is in ~/.bash_ history. From the bash man pages:

history [n]
history -c
history -d offset
history -anrw [filename]
history -p arg [arg ...]
history -s arg [arg ...]
With no options, display the command {{history}} list with line numbers.  Lines listed with a * have been modified.
An argument of n lists only the last n lines.  If the shell variable HISTTIMEFORMAT is set and not null,  it  is
used  as a format string for strftime(3) to display the time stamp associated with each displayed history entry.
No intervening blank is printed between the formatted time stamp and the history line.  If filename is supplied,
it is used as the name of the history file; if not, the value of HISTFILE is used.  Options, if supplied, have
the following meanings:
-c     Clear the history list by deleting all the entries.
-d offset
Delete the history entry at position offset.
-a     Append the ''new'' history lines (history lines entered since the beginning of the current bash  session)
to the history file.
-n     Read  the  history lines not already read from the history file into the current history list.  These are
lines appended to the history file since the beginning of the current bash session.
-r     Read the contents of the history file and use them as the current history.
-w     Write the current history to the history file, overwriting the history file's contents.
-p     Perform history substitution on the following args and display the result on the standard  output.   Does
not  store the results in the history list.  Each arg must be quoted to disable normal history expansion.
-s     Store the args in the history list as a single entry.  The last command in the history  list  is  removed
before the args are added.

If  the  HISTTIMEFORMAT  is set, the time stamp information associated with each history entry is written to the
history file.  The return value is 0 unless an invalid option is encountered, an error occurs while  reading  or
writing  the history file, an invalid offset is supplied as an argument to -d, or the history expansion supplied
as an argument to -p fails.

The history command is very useful when building shell scripts to test out the pieces and pull up a record of what was done.

Write the history of your current shell session to /tmp/hist using history -a /tmp/hist
Store the text fred flintstone in your history using history -s fred flintstone
Check the last 10 entries of you history using history -n 10


It does just what it sounds like it does, it finds files. In general the command is run in the form find <path> <filters>. Some very important ways to use find include searching for files using timestamp information or user or group ownership or with particular permissions or of a particular size or file type or having a particular SELinux context. The output of the find command is a filename with the full path. This can be fed into tools like xargs for further processing. find makes Microsoft search function look like a child's toy.



find -name Foo

Finds all files with "Foo" in name from current directory and subdirectories

find -iname foo

Finds all files with "foo" in the name with no case sensitivity: finds foo, Foo FoO, etc

find / -mindepth 2 -name passwd

Finds all files named passwd that have at least 2 "/"s in their path

find / -maxdepth 3 -name passwd

Finds all files named passwd with no more than 3 "/"s in their path

find -type TYPE

Find all files of type TYPE where TYPE is
s = socket
d = directory
f = file

find -type f -name ".*"

Find all hidden files

find -mtime 2

Find files changed more than 2*24 hours ago

find -mmin 15

Find files changed more than 15 minutes ago

find -mmin -15

Find files change less than 15 minutes ago

find -atime -1

Find files access less than 24 hours ago

try find

Find the hidden files in your home directory
Find the hidden files in your home directory than have been modified in the last 48 hours
Find the hidden directories in your home directory that have been access less than 3 days ago


xargs is a great utility for executing command from a list of input. Paired with find, for example, xargs can copy, move, or remove matched files.

By default, xargs will pass all input lines as individual parameters to one call of the specified program. With the -I {} option, xargs will invoke the specified program once per line, and replace the string {} with the input line. This is useful for programs that can only deal with one filename at a time, or where the input string cannot be the final parameter of the command.

try xargs

First create a test directory: mkdir test
Then cd into it and create three test files with touch foo bar baz.
Then run find . -name "b*", examine the output, and ensure you understand the find invocation.
Finally run the same find command, followed by | xargs -I {} rm {}.
When you are done, cd .. and rm -r test/ to remove the test directory.

Table of additional commands

More useful commands that are easy to see how to use





which command name

Search for executable command name in path


whereis command name

Finds binary and man pages for command name


locate filename

Search the locate database for file names equal to or containing filename


file filename

Determine the type of a file


ps [LIN:-aux]

Report process status


kill [LIN:-9] pid

Shut down the process with process id pid. kill -9 will kill the process even if it can't shut down gracefully.



Report free disc space


du [LIN:-sh] filename

Report how much disc space filename takes up. Filename can also be a directory.



Report memory usage on system.



Start the process monitoring program "top".


head [LIN:-n N] filename

Display the first N lines of filename. Default is 10 lines.


tail [LIN:-n N] filename

Display the last N lines of filename. Default is 10 lines.


ln [LIN:-s] filename1 filename2

Make a link to filename1 named filename2. The "-s" option makes it a symbolic link.

Basic commands exercise

Download the file and untar it. The directions are in the README



commands.tar.gz (application/x-gzip)
commands.tar.gz (application/x-gzip)