$ ls/
. You are supposed to enter the text in bold. The $
is the prompt for
your regular user account. If you see a #
as a prompt, you should be superuser. Ctrl + r
. Then you can cycle between the different commands by pressing Ctrl + r
again. command -options arguments
. Most commands use options consisting of a single option preceded by a -
. For
instance, ls -l
. But many commands also support long options, which are entire words that are preceded by two dashes instead of just one. For instance,
the long option version of ls -a
would be ls --all
. Also, many commands allow short options to be strung together. For instance, you can run
ls -lt
, where l
would give you the long form output, and t
would sort the output by the modified date of the file. You can combine the
long and the short forms together as well. For instance, ls -lt --reverse
would reverse the sorting order. fork()
and exec()
. Differences between fork and exec on SO. fork
exec
ls
command to show the contents of a directory. When you enter ls into a terminal
window, the shell that's running inside the terminal window calls fork()
to create a copy of the shell, and then the new copy of the shell calls
exec(ls)
to run ls
.
bash
or the "Bourne-again"
shell. The bash shell is the default shell on most Linux distributions, and /bin/sh
is normally a link to bash
on a Linux system. name@host:path$
, (rgbk@rgbk-VirtualBox:~$
). cat /etc/passwd
This command displays the contents of the /etc/passwd system information file and then returns your shell prompt. cat
: It outputs the contents of one or more files. The general syntax of the cat command is as follows:
cat file1 file2 ...
. When you run this command, cat prints the contents of file1, file2, and any other files that you specify (denoted by ...), and then exits.
The command is called cat because it performs concatenation when it prints the contents of more than one file.
Standard Input and Standard Output
: We'll usecat
to briefly explore Unix input and output (I/O). Unix processes use I/O
streams
to read and write data. Processes read data from input streams and write data to output streams. Streams are very flexible. For example, the source of an input stream can be
a file, a device, a terminal, or even the output stream from another process. cat
in the terminal (without specifying a filename) you won't get the shell prompt back because cat is still running. Any text that you enter in the
terminal now gets repeated back to you. The reason cat adopted an interactive behavior has to do with streams. Because you did not specify an input filename, cat read from
the standard input stream provided by the Linux kernel rather than a stream connected to a file. In this case, the standard input was connected to the terminal in
which you ran cat. ls
:
lists the contents of a directory. The default is the current directory. Use ls -l
for a detailed (long) listing and
ls -F
to display file type information. ls -lh
to print the listing with human readable file sizes. cp
:
copies files. For example, to copy file1 to file2, enter this: cp file1 file2
. This will create a new file file2 that
is a copy of file1. To copy a number of files to a directory enter cp file1 ... fileN dir
where dir is the name of the folder to which the files
will be copied to. This folder should already have been created.
mv
:
In its simplest form, it renames a file. For example, to rename file1 to file2, enter this: mv file1 file2
. This will rename
file1 to file2. You can also use mv to move a number of files to a different directory: mv file1 ... fileN dir
. Note that dir can be
relative path as well. What this means is that in order to move files one directory up, you could do mv file1 file2 ..
.
touch
:
creates a file. If the file already exists, touch does not change it, but it does update the file's modification time stamp printed
with the ls -l
command. touch file
will create a new file named file.
rm
:
To delete (remove) a file, use rm. After you remove a file, it's gone from your system and generally cannot be undeleted. rm file
will delete the file named
file.
echo
:
The echo command prints its arguments to the standard output. The echo command is very useful for finding expansions of shell globs
("wildcards" such as *) and variables (such as $HOME), which you will encounter later in this chapter. For instance check this question: ls - Not working in certain directories on SO.
/
, sometimes called the root directory
. The directory separator is the slash (/), not the backslash
(\). ..
would refer to /usr.
Similarly, ../bin
would refer to /usr/bin. .
is still /usr/lib, and ./X11
is /usr/lib/X11. You
won't have to use .
very often because most commands default to the current directory if a path doesn't start with / (you could just use X11 instead of ./X11 in
the preceding example). In other words, if you do not specify a pathname to something, the working directory will be assumed. cd
: The current working directory is the directory that a process (such as the shell) is currently in. The cd command changes the
shell's current working directory: cd dir
will change the working directory to dir. If you omit dir, the shell returns to your home
directory, the directory you started in when you first logged in. Each user account is given its own home directory, which is the only place the user is allowed
to write files when operating as a regular user.
mkdir
: creates a new directory. mkdir dir
will create a new directory named dir. Will throw error if dir already
exists.
rmdir
: removes the directory. rmdir dir
will remove the directory dir. If dir isn't empty, this command fails. rm -rf dir
to delete a directory and its contents, but be careful! This is one of the few commands that can do serious damage, especially if you
run it as the superuser. The -r
option specifies recursive delete to repeatedly delete everything inside dir, and -f
forces the delete operation.
Don't use the -rf
flags with globs such as a star (*).
Shell Globbing/Wildcards
: The shell can match simple patterns to file and directory names, a process known as globbing. The simplest of these is the glob character*
, which tells the shell to match any number of arbitrary characters. For example, running echo *
will print all the
files in the current directory. at*
expands to all filenames that start with at *at
expands to all filenames that end with at *at*
expands to all filenames that contain at *.*
to match all files. Break this habit now. In Linux and other versions of Unix, you must use
*
to match all files. In the Unix shell, *.*
matches only files and directories that contain the dot (.) character in their names. Unix filenames do
not need extensions and often do not carry them. ?
), instructs the shell to match exactly one arbitrary character. For example, b?at matches boat and brat.
grep
: The grep command prints the lines from a file or input stream that match an expression. For instance, running
grep root /etc/passwd
will print all the lines in the /etc/passwd file that contains the word root. grep root /etc/*
. -i
(for case-insensitive matches) and -v
(which inverts the search, that is, prints all lines that
don't match). There is also a more powerful variant called egrep
(which is just a synonym for grep -E
). less
: The less command comes in handy when a file is really big or when a command's output is long and scrolls off the top of the screen. To page through a
big file like /usr/share/dict/words, use the command less /usr/share/dict/words
. When running less
, you'll see the contents of the file one
screenful at a time. Press the spacebar to go forward in the file and the b
key to skip back one screenful. To quit, type q
. /word
, and to search backward, use ?word
. When you find
a match, press n
to continue searching. less
to view the output. Here's an example of sending the output of a grep command to
less
: grep ie /usr/share/dict/words | less
. pwd
: The pwd (print working directory) program simply outputs the name of the current working directory. diff
: To see the differences between two text files. diff file1 file2
will show the differences between them. Weird output though. file
: If you see a file and are unsure of its format, try using the file command to see if the system can guess. file fileName
. find
and locate
: find
is used to find a file anywhere in the entire directory tree. find dir -name file -print
will
find the file with name file anywhere within the directory dir including its sub-directories. The -name
args is used to specify the name of the
file. The -print
flag will print the path of the found file on the terminal with a newline character at the end. The -print
flag is passed in by
default even when you do not specify it in the command. See What
is the difference between find . and find . -print. locate
command for finding files. Rather than searching for a file in real time, locate
searches an index that the system
builds periodically. Searching with locate is much faster than find, but if the file you're looking for is newer than the index, locate
won't find it. whereis
is another similar useful command. For example, in order to find out where the ls
command is located, you could run
whereis ls
and that would print ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
. head
and tail
: To quickly view a portion of a file or stream of data, use the head
and tail
commands. For example,
head /etc/passwd
shows the first 10 lines of the password file, and tail /etc/passwd
shows the last 10 lines. -n
option, where n is the number of lines you want to see (for example, head -5 /etc/passwd
or
tail -5 /etc/passwd
). tail +n
. For instance, tail +25 /etc/passwd
will print the contents of the password file starting from line
25 right up to the end of the file. sort
: The sort
command puts the lines of a text file in alphanumeric order. If the file's lines start with numbers and you want to sort in
numerical order, use the -n option. The -r option reverses the order of the sort. It won't modify the actual file though.
ls -a
shows configuration files known as dot files. These are files and directories whose names begin with a dot (.). Common dot files are
.bashrc
and .login
. There are dot directories too, like .ssh
. ls
doesn't list dot
files unless you use the -a option. .*
. For instance, look at the below output. echo *
does not show the
dot files, but doing echo .*
does. But note: you can run into problems with globs because .*
matches .
and ..
(the current
and parent directories). You may wish to use a pattern such as .[^.]*
or .??*
to get all dot files except the current and parent directories. You
can see this in the below output as well.
PS1
variable before displaying the prompt. $$
is another shell variable that evaluates to the current shell's PID. variableName=value
. Note that there shouldn't be a space around the = sign. Then you
can read the value of the variable anywhere by doing echo $variableName
. export
command. For example, if you want to declare a shell variable as an environment variable, you
can do the following:
PATH
is a special environment variable that contains the command path (or path
for short). A command path is a list of system directories
that the shell searches when trying to locate a command. For example, when you run ls
, the shell searches the directories listed in PATH
for the ls
program. If programs with the same name appear in several directories in the path, the shell runs the first matching program. $PATH
, you'll see that the path components are separated by colons (:). To tell the shell to look in more places for programs, change the
PATH
environment variable. For instance, PATH=dir:$PATH
(note: no $ on the variable name on the lhs) will add dir to the path so that the
shell looks in dir before looking at other PATH
directories. Similarly, doing PATH=$PATH:dir
will add dir to the end so that the shell
looks in dir only if it has not been able to find the program in any of the other PATH
directories. export PATH
or
something after changing the PATH
, you can just close the terminal and open a new one and the PATH
would be reverted to the old value.
man commandName
in order to get more info regarding a command. For example, man ls
will give you info about the ls
command. man -k commandName
. For example, man -k sort
will print out all the commands
that contain "sort" in it along with what they do. --help
or -h
(the option varies from command to
command). info commandName
, like so: info grep
.
>
redirection character command > fileName
. For example,
cat /etc/passwd >passwords
. The shell creates fileName if it does not already exist. If fileName exists, the shell erases (clobbers) the
original file first. >>
redirection syntax: command >> fileName
. |
. For example,
head /proc/cpuinfo | tr a-z A-Z
. You can send output through as many piped commands as you wish; just add another pipe before each additional command.
Standard Error
: Occasionally, you may redirect standard output but find that the program still prints something to the terminal. This is called standard error (stderr); it's an additional output stream for diagnostics and debugging.ls /fff > newFile
. When you run this command, the newFile that is created will be empty, and you will this
message on the terminal instead: "ls: cannot access '/fff': No such file or directory". Since we only redirected the stdout stream, the stderr stream still gets
displayed on the terminal. In order to save the error message to the text file as well, we would have to redirect the stderr stream as well. We do this with:
ls /fff > newFile 2> errorFile
. The number 2 specifies the stream ID that the shell modifies. Stream ID 1 is standard output (the default), and 2 is
standard error. >&
notation. For example, to send both standard output and standard
error to the file you can do this ls /fff > newFile 2>&1
. This will send both the standard output as well as the standard error to the same file
newFile.
Standard Input Redirection
: To channel a file into a commands standard input you can use the<
operator. For example,
head < /proc/cpuinfo
. Most Unix commands accept filenames as arguments, hence this isn't very common. For example, the preceding command could have
been written as head /proc/cpuinfo
.
ps a
will show the processes currently
running and show an output like this:
Command Options
: Theps
command has many options. Here are some of the most useful combinations: ps x
: Show all of your running processes ps ax
: Show all processes on the system, not just the ones you own ps u
: Include more detailed information on processes ps w
: Show full command names, not just what fits on one line
Killing Processes
: To terminate a process, send it a signal with thekill
command. A signal is a message to a process
from the kernel. When you run kill, you're asking the kernel to send a signal to another process. Doing kill processId
will send the TERM
signal (the terminate signal) to the process with pid processId. You can send different signals by adding an extra option to kill. For example, to freeze a process
instead of terminating it, use the STOP
signal: kill -STOP processId
. A stopped process is still in memory, ready to pick up where it left
off. Use the CONT
signal to continue running the process again: kill -CONT processId
. kill
to end the process with the INT
(interrupt)
signal. KILL
signal. Other signals give the process a chance to clean up after itself, but KILL
does not. The operating system terminates the process and forcibly removes it from memory. Use this as a last resort. kill -9 processId
instead of kill -KILL processId
.
gunzip file.gz &
. The shell should respond by printing the PID of the new background process, and the prompt should return immediately so that you can continue
working. permissions
that determine whether you can read, write, or run the file. Running ls -l
displays the permissions. This
is an example:
groups
: shows which groups the currently logged in user belongs to. cat /etc/group
: lists all the groups that are present. sudo groupadd myGroup
: creates a new group named myGroup sudo usermod -a -G myGroup rgbk
: will add the user rgbk to myGroup. But doing this does not seem to show myGroup when you run
groups
. -
in this position, as in the example, denotes a regular file, meaning that there's nothing
special about the file. This is by far the most common kind of file. Directories are also common and are indicated by a d
in the file type slot. user
, group
, and other
, in that order. For
example, the rw- characters in the example are the user permissions, the r-- characters that follow are the group permissions, and the final r-- characters are the other
permissions. r
: means that the file is readable. This permission give you the authority to open and read a file. Read permission on a directory gives you the ability to
lists its content. w
: means that the file is writable. The write permission on a file gives you the authority to modify the contents of a file. The write permission on a
directory gives you the authority to add, remove and rename files stored in the directory. Consider a scenario where you have write permission on file but do not have
write permission on the directory where the file is stored. You will be able to modify the file contents. But you will not be able to rename, move or remove the file from the
directory. x
: means that the file is executable (you can run it as a program) -
: means nothing/no permission s
in the user permissions listing instead of an x
. This indicates that the executable is setuid, meaning
that when you execute the program, it runs as though the file owner is the user instead of you. Many programs use this setuid bit to run as root in order to get the
privileges they need to change system files. One example is the passwd program, which needs to change the /etc/passwd file.
chmod
command. First, pick the set of permissions that you want to change, and then pick the bit to change. For example, to add
group (g) and world (o, for "other") read (r) permissions to fileName, you could run these two commands: chmod g+r fileName
,
chmod o+r fileName
. Or you could do all of it in one shot: chmod go+r fileName
. To remove these permissions, we would do:
chmod go-r fileName
. chmod 644 fileName
. This is called an absolute change because it sets all permission
bits at once. umask
.
l
as the file type for symLinkToNewFolder1. If you try to do
cd symLinkToNewFolder1
, you will enter the /home/rgbk/Desktop/MyStuff/newFolder1
instead. Symbolic links are simply names that point to other names.
Their names and the paths to which they point don't have to mean anything. For example, /home/rgbk/Desktop/MyStuff/newFolder1
doesn't even need to exist.
ln -s target linkname
where linkname is the name of the symbolic link, and target is the path of the file
or directory that the link is meant to point to. The -s
is meant to say that the link is a symbolic link. Don't forget the -s option when creating a symbolic
link. Without it, ln creates a hard link, giving an additional real filename to a single file. And this is supposed to be bad for reasons that are beyond me.
tar
puts multiple files into a single (tar) file (archiving) gzip
compresses one file (compressing) tar
to get all the files into a single file, let's say archive.tar, and then
compress it by calling gzip
on it to convert it into archive.tar.gz. tar
on it. You can just run gzip
on it directly.
gzip
Compress a file:
gzip -k file1
. k
to keep the original uncompressed file. Decompress a file:
gunzip file1.gz
.gz
is a GNU Zip archive. Use gunzip file.gz
to uncompress file.gz and remove the suffix; to compress it again,
use gzip file
. gzip file1 file2
will create two separate files file1.gz and file2.gz. Note that this will also delete file1 and file2. In
order to keep the original files along with the newly created compressed files, run with -k
option: gzip -k file1 file2
as mentioned in this SO
answer: How to tell gzip to keep original file?
tar
Archive files:
tar cvf myArchive.tar file1 file2 ...
. c for create, v for verbose, f for file name. Creates archive called
myArchive.tar Unpack archive:
tar xvf myArchive.tar
. x for extract, v for verbose, f to pass in the archive name to unpack. Won't work without
the f flag. See contents of archive:
tar -tf myArchive.tar
. t to list the contents of the archive.
tar cvf myArchive.tar file1 file2 ...
to archive files. This will create a new archive named myArchive. The -c
flag tells it to create
a new archive. The -v
flag gives a verbose representation of the output. -f
is used to tell it what the name of the final archived file should be.
If you are using the -f
flag, make sure that the next argument that you pass in is the file name that you want the archive to have. Otherwise, if you erroneously
do something like this tar cvf file1 file2 ...
, your file1 will be overwritten with the archived file2. Also, if you do something like this:
tar -cvfp newArchive.tar file1 file2
, you will end up creating a new archive named p instead of the terminal reading p as a flag that you are
passing. Hence, run this instead tar -cvpf newArchive.tar file1 file2
. This will correctly create a new archive that is named newArchive.tar and has the
files that you need. Else you can also run something like this: tar -cv -f archive3.tar file1 file2
tar xvf myArchive.tar
. The x
flag puts tar into extract (unpack) mode. When using extract mode,
remember that tar does not remove the archived .tar file after extracting its contents.
Table-of-Contents Mode
:t
flag instead of the x
flag. This will print the files within it so that when you unpack, you will have a manageable mess on your hands. tar -tf myArchive.tar
will show the contents of
the myArchive.tar archive. Doing tar -tvf myArchive.tar
will show some other details as well. -p
flag. Refer Copy files without losing file/folder permissions,
How to tar a directory preserving not only
permissions, but ownership too,
What does tar's -p (preserve permissions) flag actually
preserve?. Also, when you are unpacking the files, you will want to be logged in as the root user, because a user cannot change the permissions of another user.
Archiving and Compressing files in one go
:tar zcvf allInOne.tar.gz file1 file2
Decompressing and unpacking files in one go
:tar zxvf allInOne.tar.gz
.gz
first and then worry about the .tar
. For instance, to decompress and unpack something like myFile.tar.gz, you
would first run gunzip myFile.tar.gz
to decompress it, and then tar xvf myFile.tar
to unpack it. zcat
command. zcat file.tar.gz | tar xvf -
is a command pipeline that will unpack file.tar.gz. The zcat
command is the same as gunzip -dc
.
The -d
option decompresses and the -c
option sends the result to standard output (in this case, to the tar
command). -
passed in to the tar command above? Some commands accept -
in place of a filename to read from standard input instead of from a
named file. This is what the -
argument passed to tar
after xzf
is. Read What does
the hyphen "-" mean in "tar xzf -"? for details. zcat
, the version of tar
that comes with Linux has a shortcut. You can use z
as an option to
automatically invoke gzip
on the archive; this works both for extracting an archive (with the x
or t
modes in tar
) and
creating one (with c
). /usr
contains some of the same directory names as /
Important subdirectories in root
:/bin
: Contains ready-to-run programs (also known as an executables), including most of the basic Unix commands such as ls and cp.
Most of the programs in /bin are in binary format, having been created by a C compiler, but some are shell scripts in modern systems. /dev
: Contains device files. We will look at these in Chapter 3. /etc
: This core system configuration directory (pronounced EHT-see) contains the user password, boot, device, networking, and other setup files. Many items in
/etc are specific to the machine's hardware. /etc/passwd contains a list of the user accounts /home
: Holds personal directories for regular users. Most Unix installations conform to this standard. Ordinary users can write files only in their
home directories. This limitation protects the system from errant user activity. /lib
: An abbreviation for library, this directory holds library files containing code that executables can use. There are two types of libraries:
static and shared. The /lib directory should contain only shared libraries, but other lib directories, such as /usr/lib, contain both varieties and other auxiliary files.
/proc
: Provides systems statistics through a browsable directory-and-file interface. The /proc directory contains information about currently running
processes as well as some kernel parameters. It's not a real filesystem in the sense of files stored on your hard drive. Rather, it is a virtual filesystem maintained by the
Linux kernel. The "files" it contains are peepholes into the kernel itself that can tell you how the kernel is performing. /sys
: This directory is similar to /proc in that it provides a device and system interface. We will look at it in more detail in Chapter 3. /sbin
: The place for system executables. Programs in /sbin directories relate to system management, so regular users usually do not have /sbin components in
their command paths. Many of the utilities found here will not work if you're not running them as root. /tmp
: A storage area for smaller, temporary files that you don't care much about. Any user may read to and write from /tmp, but the user may not have
permission to access another user's files there. Many programs use this directory as a workspace. If something is extremely important, don't put it in /tmp because most
distributions clear /tmp when the machine boots and some even remove its old files periodically. Also, don't let /tmp fill up with garbage because its space is usually
shared with something critical (like the rest of /, for example). /usr
: Although pronounced "user", this subdirectory has no user files. Instead, it contains a large directory hierarchy, including the bulk of the Linux
system. Many of the directory names in /usr are the same as those in the root directory (like /usr/bin and /usr/lib), and they hold the same type of files. (The reason that
the root directory does not contain the complete system is primarily historic-in the past, it was to keep space requirements low for the root.) /var
: The variable subdirectory, where programs record runtime information. System logging, user tracking, caches, and other files that system
programs create and manage are here.
Other Root Subdirectories
: There are a few other interesting subdirectories in the root directory:/boot
: Contains kernel bootloader files. These files pertain only to the very first stage of the Linux startup procedure. /media
: A base attachment point for removable media such as flash drives that is found in many distributions /opt
: This may contain additional third-party software. Many systems don't use /opt.
Subdirectories in /usr
: /usr is where most of the user-space programs and data reside. In addition to /usr/bin, /usr/sbin, and /usr/lib, /usr contains the following:/include
: Holds header files used by the C compiler. What are header files in C? This YT video
explains how it works. /info
: Contains GNU info manuals /local
: Is where administrators can install their own software. Its structure should look like that of / and /usr. What does that mean? /man
: Contains manual pages.
Kernel Location
: On Linux systems, the kernel is normally in /vmlinuz or /boot/vmlinuz. A boot loader loads this file into memory and sets it in motion when the system boots. We will read more about bootloader in Chapter 5. vmlinuz is the name of the Linux kernel executable. vmlinuz is a compressed Linux kernel, and it is capable of loading the operating system into memory so that the computer becomes usable and application programs can be run.su
and enter the root password in order to start a root shell. This way you can run commands as the superuser. However, this has some
drawbacks: sudo
: sudo allows administrators to run commands as root when they are logged in as themselves. When you run a command withsudo
,
sudo logs this action with the syslog service under the local2 facility. We will look at this later.
Who can run sudo
:
sudo
, you must be configured as a privileged user in your
/etc/sudoers
file.
echo string1 string 2 > /dev/null
, this will redirect the stderr output to a file. In this case, the "file" is
actually a device: /dev/null
. Now it is upto the kernel to decide what it should do with the data written to this device. In the case of /dev/null, the kernel
simply ignores the input and throws away the data. Why would you need something like /dev/null? It is useful in case you are running a command and want to ignore the stderr
stream for example. So ssh myhostname "command1; command2;...commandn;" 2>/dev/null
will send the error stream to /dev/null which will be ignored, and you
will only see stdout on the terminal. What does "/dev/null" mean
at the end of shell commands, and also What is /dev/null. Block Device
Character Device
Pipe Device
|
to redirect the output of one command into the input of another command? Like so:
ls -l | grep newFolder
. In this case, the pipe that we were using is known as an anonymous/unnamed pipe. It is called anonymous because the pipe exists only
inside the kernel and cannot be accessed by processes that created it, in this case, the bash shell. The other sort of pipe is a "named" pipe, which is sometimes called a
FIFO. FIFO stands for "First In, First Out" and refers to the property that the order of bytes going in is the same coming out. The "name" of a named pipe is actually a
file name within the file system. Some relevant source. mkfifo pipe1
will create a new pipe device. Source on Baeldung.
Socket Device
ls -l /sys/block
to reveal the true sysfs paths.
dd
reads from an input file or stream and writes to an output file or stream, possibly doing some encoding conversion on the way.