Basic everyday commands
ls
] [ echo
] [ cat
] [ cp
] [ mv
] [ mkdir
] [ cd
] [ tar
] [ touch
] [ cut
] [ history
] [ find
] [ xargs
] [ Table of additional commands ] [ Basic commands exercise ]ls
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
Try running |
echo
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 |
cat
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 |
cp
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 timestampscp -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 withls -l
andcat
. 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 withls -l
and note what changed.
mv
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 |
mkdir
mkdir
is used to m
ak
e dir
ectories:
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
|
cd
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 willcd
(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. Typeecho $OLDPWD
to see it)
try cd
|
tar
t
ape ar
chive 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. |
touch
this will create an empty file with the given filename or update the timestamp on an existing file.
try touch
|
cut
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 |
history
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 |
find
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.
command |
explanation |
---|---|
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 |
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 |
xargs
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: |
Table of additional commands
More useful commands that are easy to see how to use
command |
syntax |
explanation |
---|---|---|
which |
which command name |
Search for executable command name in path |
whereis |
whereis command name |
Finds binary and man pages for command name |
locate |
locate filename |
Search the locate database for file names equal to or containing filename |
file |
file filename |
Determine the type of a file |
ps |
ps [LIN:-aux] |
Report process status |
kill |
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. |
df |
df |
Report free disc space |
du |
du [LIN:-sh] filename |
Report how much disc space filename takes up. Filename can also be a directory. |
free |
free |
Report memory usage on system. |
top |
top |
Start the process monitoring program "top". |
head |
head [LIN:-n N] filename |
Display the first N lines of filename. Default is 10 lines. |
tail |
tail [LIN:-n N] filename |
Display the last N lines of filename. Default is 10 lines. |
ln |
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 |