Linux Basics
Linux Basics
Linux Basics
Character Description
Escape character. If you want to reference a special
\ character, you must “escape” it with a backslash first.
Example: touch /tmp/filename\*
Directory separator, used to separate a string of directory
/ names.
Example: /usr/src/linux
Current directory. Can also “hide” files when it is the
.
first character in a filename.
.. Parent directory
~ User's home directory
Represents 0 or more characters in a filename, or by
itself, all files in a directory.
*
Example: pic*2002 can represent the files pic2002,
picJanuary2002,picFeb292002, etc.
Represents a single character in a filename.
? Example: hello?.txt can represent hello1.txt, helloz.txt,
but not hello22.txt
Can be used to represent a range of values, e.g. [0-9], [A-
Z], etc.
[ ]
Example: hello[0-2].txt represents the names
hello0.txt,hello1.txt, and hello2.txt
“Pipe”. Redirect the output of one command into another
| command.
Example: ls | more
Redirect output of a command into a new file. If the file
> already exists, over-write it.
Example: ls > myfiles.txt
Redirect the output of a command onto the end of an
>> existing file.
Example: echo .Mary 555-1234. >> phonenumbers.txt
Redirect a file as input to a program.
<
Example: more < phonenumbers.txt
Command separator. Allows you to execute multiple commands
; on a single line.
Example: cd /var/log ; less messages
Command separator as above, but only runs the second
command if the first one
&&
finished without errors.
Example: cd /var/logs && less messages
Execute a command in the background, and immediately get
& your shell back.
Example: find / -name core > /tmp/corefiles.txt &
2
The LINUX Directory Layout
Directory Description
To change directory
To return to home directory
cd
To change into specified directory name
cd <directory name>
4
To change to home or other path relative to home.
“~” is an alias for your home directory
cd ~
To move up one directory
cd ..
To return to previous directory
cd -
To create a file
cat > filename.txt
//type the contents and press Ctrl+D to exit
To create a directory
mkdir <directory name>
: mkdir dhan
mkdir -p <directory path>
: mkdir -p /home/sng/dhan
To create directories in multiple paths
mkdir -p parent/{child1,child2,child3}
: mkdir -p work/{in,out,pending}/{a,b,c}
To delete a directory
rmdir <directory name>
: rmdir /tmp/myfiles/
To locate the program, source code, and manual page for a command
whereis <command name>
6
To search for files anywhere on the file system
locate <keyword to search>
//find all files and directories that contain the keyword
In MB
du -h
7
To display CPU processes in a full-screen GUI
top
// type “Q” to quit
8
To search a file for a particular pattern
grep [-R] <word to search> <file name/directory name>
: grep science science.txt
//case-sensitive search
: grep -i science science.txt
//case-insensitive search
: grep -i ‘spinning top’ science.txt
//multiword search
: grep -R passwd /etc/
//search in directory
To switch user
su <user name>
//to switch to a particular user account
su
//to switch to root account
su -
//to switch to root, and log in with root's environment
9
To display the name of currently working terminal
tty
To block a user
passwd -l <user name>
To unblock a user
passwd -u <user name>
To view IP address
ifconfig
11
To print the date
date
Signals to a process
To list all signals
kill -l
To kill a process
kill <process id>
12
To terminate a process
kill -9 <process id>
To normally exit a process
kill -15 <process id>
13