Linux Commands Flashcards
cal
Shows system calendar
date
Shows date
df
To see the current amount of free space on your disk drives
free
to display the amount of free memory
exit
to end a terminal session
Ctrl- Alt-F1 through Ctrl-Alt-F6
To access virtual terminals or virtual consoles
Alt and F1-F6
To switch from one virtual console to another
Alt-F7
To return to the graphical desk- top
pwd
Print name of current working directory
cd (argument)
Change directory
ls
List current working directory contents
.
The “.” symbol refers to the working directory
..
the “..” symbol refers to the working directory’s parent directory
cd
Changes the working directory to your home directory
cd -
Changes the working directory to the previous working directory
cd ~user_name
Changes the working directory to the home directory of user_name
ls -a
Shows hidden files
ls (arg) (arg) …
To list the contents of one or more directories
ls -l
By adding “-l” to the command, we changed the output to the long format
command -options arguments
Overall style of a command
ls -lt
many commands allow multiple short options to be strung together. In this example, the ls command is given two options, the “l” option to produce long format output, and the “t” option to sort the result by the file’s modification time
ls -lt –reverse
Long options are preceded with two dashes
ls -a
ls –all
List all files, even those with names that begin with a period, which are normally not listed (i.e., hidden)
ls -A
ls –almost-all
Like the -a option above except it does not list . (current directory) and .. (parent directory)
ls -d
ls –directory
Ordinarily, if a directory is specified, ls will list the contents of the directory, not the directory itself. Use this option in conjunction with the -l option to see details about the directory rather than its contents
ls -F
ls –classify
This option will append an indicator character to the end of each listed name. For example, a “/” if the name is a directory
ls -h
ls –human-readable
In long format listings, display file sizes in human readable format rather than in bytes
ls -l
Display results in long format
ls -r
ls –reverse
Display the results in reverse order. Normally, ls displays its results in ascending alphabetical order
ls -S
Sort results by file size
ls -t
Sort by modification time
ls Long Listing Fields:
-rw-r–r–
Access rights to the file. The first character indicates the type of file. Among the different types, a leading dash means a regular file, while a “d” indicates a directory.
The next three characters are the access rights for the file’s owner, the next three are for members of the file’s group, and the final three are for everyone else. The full meaning of this is discussed in Chapter 9 – Permissions
ls Long Listing Fields:
1
File’s number of hard links. See the discussion of links later in this chapter
ls Long Listing Fields:
root
The username of the file’s owner
ls Long Listing Fields:
root
The name of the group which owns the file
ls Long Listing Fields:
32059
Size of the file in bytes
ls Long Listing Fields:
2007-04-03 11:05
Date and time of the file’s last modification
ls Long Listing Fields:
oo-cd-cover.odf
Name of the file
file (filename)
the file command will print a brief description of the file’s contents
less (filename)
the less program allows you to scroll forward and backward through a text file.
Once the less program starts, we can view the contents of the file. If the file is longer
than one page, we can scroll up and down. To exit less, press the “q” key.
less Commands:
Page Up or b
Scroll back one page
less Commands:
Page Down or space
Scroll forward one page
less Commands:
Up Arrow
Scroll up one line
less Commands:
Down Arrow
Scroll down one line
less Commands:
G
Move to the end of the text file
less Commands:
1G or g
Move to the beginning of the text file
less Commands:
/characters
Search forward to the next occurrence of characters
less Commands:
n
Search for the next occurrence of the previous search
less Commands:
h
Display help screen
less Commands:
q
Quit less
Directories Found On Linux Systems:
/
The root directory. Where everything begins.
Directories Found On Linux Systems:
/bin
Contains binaries (programs) that must be present for the system to boot and run.
Directories Found On Linux Systems:
/boot
Contains the Linux kernel, initial RAM disk image (for drivers needed at boot time), and the boot loader.
Interesting files:
● /boot/grub/grub.conf or menu.lst, which
are used to configure the boot loader.
● /boot/vmlinuz, the Linux kernel
Directories Found On Linux Systems:
/dev
This is a special directory which contains device nodes. “Everything is a file” also applies to devices. Here is where the kernel maintains a list of all the devices it understands.
Directories Found On Linux Systems:
/etc
The /etc directory contains all of the system-wide configuration files. It also contains a collection of shell scripts which start each of the system services at boot time. Everything in this directory should be readable text.
Interesting files: While everything in /etc is interesting, here are some of my all-time favorites:
● /etc/crontab, a file that defines when automated jobs will run.
● /etc/fstab, a table of storage devices and their associated mount points.
● /etc/passwd, a list of the user accounts.
Directories Found On Linux Systems:
/home
In normal configurations, each user is given a directory in /home. Ordinary users can only write files in their home directories. This limitation protects the system from errant user activity.
Directories Found On Linux Systems:
/lib
Contains shared library files used by the core system programs. These are similar to DLLs in Windows.
Directories Found On Linux Systems:
/lost+found
Each formatted partition or device using a Linux file system, such as ext3, will have this directory. It is used in the case of a partial recovery from a file system corruption event. Unless something really bad has happened to your system, this directory will remain empty.
Directories Found On Linux Systems:
/media
On modern Linux systems the /media directory will contain the mount points for removable media such as USB drives, CD-ROMs, etc. that are mounted automatically at insertion.
Directories Found On Linux Systems:
/mnt
On older Linux systems, the /mnt directory contains mount points for removable devices that have been mounted
manually.
Directories Found On Linux Systems:
/opt
The /opt directory is used to install “optional” software. This is mainly used to hold commercial software products that may be installed on your system.
Directories Found On Linux Systems:
/proc
The /proc directory is special. It’s not a real file system in the sense of files stored on your hard drive. Rather, it is a virtual file system maintained by the Linux kernel. The “files” it contains are peepholes into the kernel itself. The files are readable and will give you a picture of how the kernel sees your computer.
Directories Found On Linux Systems:
/root
This is the home directory for the root account.
Directories Found On Linux Systems:
/sbin
This directory contains “system” binaries. These are programs that perform vital system tasks that are generally reserved for the superuser.
Directories Found On Linux Systems:
/tmp
The /tmp directory is intended for storage of temporary, transient files created by various programs. Some configurations cause this directory to be emptied each time the system is rebooted.
Directories Found On Linux Systems:
/usr
The /usr directory tree is likely the largest one on a Linux system. It contains all the programs and support files used by regular users.
Directories Found On Linux Systems:
/usr/bin
/usr/bin contains the executable programs installed by your Linux distribution. It is not uncommon for this directory to hold thousands of programs.
Directories Found On Linux Systems:
/usr/lib
The shared libraries for the programs in /usr/bin.
Directories Found On Linux Systems:
/usr/local
The /usr/local tree is where programs that are not included with your distribution but are intended for system- wide use are installed. Programs compiled from source code are normally installed in /usr/local/bin. On a newly installed Linux system, this tree exists, but it will be empty until the system administrator puts something in it.
Directories Found On Linux Systems:
/usr/sbin
Contains more system administration programs.
Directories Found On Linux Systems:
/usr/share
/usr/share contains all the shared data used by programs in /usr/bin. This includes things like default configuration files, icons, screen backgrounds, sound files, etc.
Directories Found On Linux Systems:
/usr/share/doc
Most packages installed on the system will include some kind of documentation. In /usr/share/doc, we will find documentation files organized by package.
Directories Found On Linux Systems:
/var
With the exception of /tmp and /home, the directories we have looked at so far remain relatively static, that is, their contents don’t change. The /var directory tree is where data that is likely to change is stored. Various databases, spool files, user mail, etc. are located here.
Directories Found On Linux Systems:
/var/log
/var/log contains log files, records of various system activity. These are very important and should be monitored from time to time. The most useful one is /var/log/messages. Note that for security reasons on some systems, you must be the superuser to view log files .
Wildcards:
*
Matches any characters
Wildcards:
?
Matches any single character
Wildcards:
[characters]
Matches any character that is a member of the set characters (just one character)
Wildcards:
[!characters]
Matches any character that is not a member of the set
characters
Wildcards:
[[:class:]]
Matches any character that is a member of the specified class
Commonly Used Character Classes:
[:alnum:]
Matches any alphanumeric character
Commonly Used Character Classes:
[:alpha:]
Matches any alphabetic character
Commonly Used Character Classes:
[:digit:]
Matches any numeral
Commonly Used Character Classes:
[:lower:]
Matches any lowercase letter
Commonly Used Character Classes:
[:upper:]
Matches any uppercase letter
mkdir (arg)…
The mkdir command is used to create directories
cp item1 item2
to copy the single file or directory “item1” to file or directory “item2”.
(If file2 exists, it is overwritten with the contents of file1)
cp item… directory
to copy multiple items (either files or directories) into a directory.
(Directory must already exist)
cp -a
cp –archive
Copy the files and directories and all of their attributes, including ownerships and permissions. Normally, copies take on the default attributes of the user performing the copy.
cp -i
cp –interactive
Before overwriting an existing file, prompt the user for confirmation. If this option is not specified, cp will silently overwrite files.
cp -r
cp –recursive
Recursively copy directories and their contents. This option (or the -a option) is required when copying directories.
cp -u
cp –update
When copying files from one directory to another, only copy files that either don’t exist, or are newer than the existing corresponding files, in the destination directory.
cp -v
cp –verbose
Display informative messages as the copy is performed.
mv item1 item2
to move or rename file or directory “item1” to “item2”
mv item… directory
to move one or more items from one directory to another.
mv -i
mv –interactive
Before overwriting an existing file, prompt the user for confirmation. If this option is not specified, mv will silently overwrite files.
mv -u
mv –update
When moving files from one directory to another, only move files that either don’t exist, or are newer than the existing corresponding files in the destination directory.
mv -v
mv –verbose
Display informative messages as the move is performed.
cp -r dir1 dir2
Copy the contents of directory dir1 to directory dir2. If directory dir2 does not exist, it is created and, after the copy, will contain the same contents as directory dir1.
If directory dir2 does exist, then directory dir1 (and its contents) will be copied into dir2.
mv dir1 dir2
If directory dir2 does not exist, create directory dir2 and move the contents of directory dir1 into dir2 and delete directory dir1.
If directory dir2 does exist, move directory dir1 (and its contents) into directory dir2.
rm (arg)…
The rm command is used to remove (delete) files and directories
rm -i
rm –interactive
Before deleting an existing file, prompt the user for confirmation. If this option is not specified, rm will silently delete files.
rm -r
rm –recursive
Recursively delete directories. This means that if a directory being deleted has subdirectories, delete them too. To delete a directory, this option must be specified.
rm -f
rm –force
Ignore nonexistent files and do not prompt. This overrides the –interactive option.
rm -v
rm –verbose
Display informative messages as the deletion is performed.
ln file link
To create a hard link
ln -s item link
to create a symbolic link where “item” is either a file or a directory.
ls -i
Show the inode(the block chain of memory where the contents part of the file and not the name part or hard link lives) number.
type command
The type command is a shell builtin that displays the kind of command the shell will
execute, given a particular command name. ( shell built in, executable program, shell function, alias)
which command
Display An Executable’s Location ( works only on executable commands not other forms)
help command
bash has a built-in help facility available for each of the shell builtins. To use it, type
“help” followed by the name of the shell builtin.
command –help
Many executable programs support a “–help” option that displays a description of the
command’s supported syntax and options.
man program
Most executable programs intended for command line use provide a formal piece of doc- umentation called a manual or man page. A special paging program called man is used to view them.
apropos term
man -k term
It is also possible to search the list of man pages for possible matches based on a search term.
whatis command
The whatis program displays the name and a one line description of a man page match- ing a specified keyword
info program
The GNU Project provides an alternative to man pages for their programs, called “info.” Info pages are displayed with a reader program named, appropriately enough, info. Info pages are hyperlinked much like web pages.
info Commands:
?
Display command help
info Commands:
PgUp or Backspace
Display previous page
info Commands:
Enter
Follow the hyperlink at the cursor location
info Commands:
PgDn or Space
Display next page
info Commands:
n
Next - Display the next node
info Commands:
p
Previous - Display the previous node
info Commands:
u
Up - Display the parent node of the currently displayed node, usually a menu.
info Commands:
q
Quit
info coreutils
will display a menu page with hyperlinks to each program contained in the coreutils
package.
/usr/share/doc
gzip
zless
Many software packages installed on your system have documentation files residing in the /usr/share/doc directory. Most of these are stored in plain text format and can be viewed with less. Some of the files are in HTML format and can be viewed with a web browser. We may encounter some files ending with a “.gz” extension. This indicates that they have been compressed with the gzip compression program. The gzip package includes a special version of less called zless that will display the contents of gzip- compressed text files.
alias name=’string’
After the command “alias” we give alias a name followed immediately (no whitespace al- lowed) by an equals sign, followed immediately by a quoted string containing the mean- ing to be assigned to the name. After we define our alias, it can be used anywhere the shell would expect a command.
unalias (arg)
To remove an alias, the unalias command is used
alias
To see all the aliases defined in the environment, use the alias command without arguments.
>
To redirect standard output to another file instead of the screen, we use the “>” redirection operator followed by the name of the file.
> file-name
to actually truncate a file (or create a new, empty file)
> >
Using the “»” operator will result in the output being appended to the file. If the file does not already exist, it is created just as though the “>” operator had been used.
(file descriptor)
A program can produce output on any of several numbered file streams. While we have referred to the first three of these file streams as standard input, output and error, the shell references them internally as file de- scriptors 0, 1 and 2, respectively. The shell provides a notation for redirecting files using the file descriptor number.
2>
Since standard error is the same as file descriptor number 2, we can redirect standard error with this notation.
example: ls -l /bin/usr > ls-output.txt 2>&1
Using this method, we perform two redirections. First we redirect standard output to the file ls-output.txt and then we redirect file descriptor 2 (standard error) to file de- scriptor one (standard output) using the notation 2>&1.
(Order is significant)
example: ls -l /bin/usr &> ls-output.txt
In this example, we use the single notation &> to redirect both standard output and stan- dard error to the file ls-output.txt. You may also append the standard output and standard error streams to a single file like so:
ls -l /bin/usr &» ls-output.txt
example: ls -l /bin/usr 2> /dev/null
Sometimes “silence is golden,” and we don’t want output from a command, we just want to throw it away. This applies particularly to error and status messages. The system pro- vides a way to do this by redirecting output to a special file called “/dev/null”. This file is a system device called a bit bucket which accepts input and does nothing with it. To sup- press error messages from a command, we do this.
/dev/null
This file is a system device called a bit bucket which accepts input and does nothing with it.
cat [file…]
The cat command reads one or more files and copies them to standard output.
example: cat movie.mpeg.0* > movie.mpeg
Since cat can accept more than one file as an argument, it can also be used to
join files together. Say we have downloaded a large file that has been split into multiple parts (multimedia files are often split this way on Usenet), and we want to join them back together. If the files were named:
movie.mpeg.001 movie.mpeg.002 … movie.mpeg.099
we could join them back together with this command.
Since wildcards always expand in sorted order, the arguments will be arranged in the cor- rect order.
cat
Enter a text by keyboard and hit Cntl-d to denote the end of the text.
If not redirected anywhere cat will send the output to screen, if redirected to a file, you have made a text file.
cat > file
Cat reads its input not from keyboard but from a file.
command1 | command2
Using the pipe operator “|” (vertical bar), the
standard output of one command can be piped into the standard input of another.
sort (input…)
Sort lines of text.
Sophisticated example: ls /bin /usr/bin | sort | less
uniq (input)
The uniq command is often used in conjunction with sort. uniq accepts a sorted list of data from either standard input or a single filename argument (see the uniq man page for details) and, by default, removes any duplicates from the list.
uniq -d
If we want to see the list of duplicates instead, we add the “-d” option to uniq.
wc
wc -l
The wc (word count) command is used to display the number of lines, words, and bytes contained in files. if executed without command line arguments, wc accepts standard input. The “-l” option limits its output to only report lines. Adding it to a pipeline is a handy way to count things.
grep pattern [file…]
When grep encounters a “pattern” in the file, it prints out the lines containing it.
grep -i
grep -v
“-i” which causes grep to ignore case when performing the search (normally searches are case sensitive) and “-v” which tells grep to only print lines that do not match the pattern.
example: head -n 5 ls-output.txt
example: tail -n 5 ls-output.txt
example: ls /usr/bin | tail -n 5
The head command prints the first ten lines of a file and the tail command prints the last ten lines. By default, both commands print ten lines of text, but this can be adjusted with the “-n” option.
tail -f
tail has an option which allows you to view files in real-time. This is useful for watch- ing the progress of log files as they are being written.
Using the “-f” option, tail continues to monitor the file and when new lines are ap- pended, they immediately appear on the display. This continues until you type Ctrl-c.
tee
The tee program reads standard input and copies it to both standard output (allowing the data to continue down the pipeline) and to one or more files. This is useful for capturing a pipeline’s contents at an intermediate stage of processing.
example: ls /usr/bin | tee ls.txt | grep zip
Tilde Expansion:
echo ~
/home/me
echo ~foo
/home/foo
Arithmetic Expansion:
$((expression))
example: echo $(((5**2) * 3))
Operators: +, -, *, /, %, **
Operates on integers only.
echo (arg)
echo is a shell builtin that performs a very simple task. It prints out its text arguments on standard out- put.
Brace Expansion:
Patterns to be brace expanded may contain a leading portion called a preamble and a trailing portion called a postscript. The brace expression itself may contain either a comma-separated list of strings, or a range of integers or single characters. The pattern may not contain embedded whitespace.
Examples:
[me@linuxbox ~]$ echo Front-{A,B,C}-Back Front-A-Back Front-B-Back Front-C-Back
[me@linuxbox ~]$ echo Number_{1..5}
Number_1 Number_2 Number_3 Number_4 Number_5
[me@linuxbox ~]$ echo {01..15}
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
[me@linuxbox ~]$ echo {001..15}
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015
[me@linuxbox ~]$ echo {Z..A}
Z Y X W VU T S R Q P O N M L K J I H G F E D C B A
[me@linuxbox ~]$ echo a{A{1,2},B{3,4}}b
aA1b aA2b aB3b aB4b
mkdir {2007..2009}-{01..12}
ls
2007-01 2007-07 2008-01 2008-07 2009-01 2009-07 2007-02 2007-08 2008-02 2008-08 2009-02 2009-08 2007-03 2007-09 2008-03 2008-09 2009-03 2009-09 2007-04 2007-10 2008-04 2008-10 2009-04 2009-10 2007-05 2007-11 2008-05 2008-11 2009-05 2009-11 2007-06 2007-12 2008-06 2008-12 2009-06 2009-12
Parameter Expansion:
Many of its capabilities have to do with the system’s ability to store small chunks of data and to give each chunk a name. Many such chunks, more properly called variables, are available for your examination.
example: echo $USER
To see a list of available variables, try this:
printenv | less
Command Substitution:
Command substitution allows us to use the output of a command as an expansion.
Examples:
[me@linuxbox ~]$ echo $(ls)
Desktop Documents ls-output.txt Music Pictures Public Templates Videos
[me@linuxbox ~]$ ls -l $(which cp)
-rwxr-xr-x 1 root root 71516 2007-12-05 08:58 /bin/cp
[me@linuxbox ~]$ file $(ls -d /usr/bin/* | grep zip)
example:
[me@linuxbox ~]$ ls -l which cp
-rwxr-xr-x 1 root root 71516 2007-12-05 08:58 /bin/cp
There is an alternate syntax for command substitution in older shell programs which is also supported in bash. It uses back-quotes instead of the dollar sign and parentheses.