Common commands:
Files and directories:
# cd /home
enter the
‘/home’ directory
‘/home’ directory
# cd ..
go back to the parent directory
# cd ../..
go back two directory levels
# cd –
go back to the previous directory
# cp file1 file2
copy file1 to file2
# cp -a dir1 dir2 copy a directory
# cp -a /tmp/dir1 . copy a directory to the current working directory (. represents the current directory)
# ls
view files in the directory
# ls -a
show hidden files
# ls -l
Show detailed information
# ls -lrt
Show files by time (l means detailed list, r means reverse order, t means sort by time)
# pwd
Show the working path
# mkdir dir1
Create
the ‘dir1’ directory
the ‘dir1’ directory
# mkdir dir1 dir2 Create two directories at the same time
# mkdir -p /tmp/dir1/dir2 Create a directory tree
# mv dir1 dir2
Move/rename a directory
# rm -f file1
Delete
‘file1’
‘file1’
# rm -rf dir1
Delete
the ‘dir1’ directory and its subdirectory contents
the ‘dir1’ directory and its subdirectory contents
View file contents:
# cat file1
view the contents of a file forward starting from the first byte
# head -2 file1 view the first two lines of a file
# more file1
view the contents of a long file
# tac file1
view the contents of a file in reverse starting from the last line
# tail -3 file1
view the last three lines of a file
Text processing:
# grep str /tmp/test find “str” in the file
‘/tmp/test’
‘/tmp/test’
# grep ^str /tmp/test find lines in the file
‘/tmp/test’ that begin with “str”
‘/tmp/test’ that begin with “str”
# grep [0-9] /tmp/test find
all lines in the file ‘/tmp/test’ that contain numbers
all lines in the file ‘/tmp/test’ that contain numbers
# grep str -r /tmp/* find “str” in the directory
‘/tmp’ and its subdirectories
‘/tmp’ and its subdirectories
# diff file1 file2 find the differences between two files
# sdiff file1 file2 display the differences between two files side by side
Find:
# find / -name file1
starting from
‘/’, enter the root filesystem to search for files and directories
‘/’, enter the root filesystem to search for files and directories
# find / -user user1
find files and directories belonging to user
‘user1’
‘user1’
# find /home/user1 -name *.bin
in directory
‘/ home/user1’, find files ending with ‘.bin’
‘/ home/user1’, find files ending with ‘.bin’
# find /usr/bin -type f -atime +100
find executable files that have not been used in the past 100 days
# find /usr/bin -type f -mtime -10
find files created or modified within the last 10 days
# locate *.ps
find files ending with
‘.ps’; first run the
‘updatedb’ command
‘.ps’; first run the
‘updatedb’ command
# find -name ‘*.[ch]’ | xargs grep -E
‘expr’ search for
‘expr’
in all .c and .h files in the current directory and its subdirectories
‘expr’
‘expr’
in all .c and .h files in the current directory and its subdirectories
# find -type f -print0 | xargs -r0 grep -F
‘expr’ search for
‘expr’
in regular files in the current directory and its subdirectories
‘expr’
‘expr’
in regular files in the current directory and its subdirectories
# find -maxdepth 1 -type f | xargs grep -F
‘expr’ search for
‘expr’
in the current directory
‘expr’
‘expr’
in the current directory
Compression and decompression:
# bzip2 file1
compress
file1
file1
# bunzip2 file1.bz2
decompress
file1.bz2
file1.bz2
# gzip file1
compress
file1
file1
# gzip -9 file1
compress
file1
as much as possible
file1
as much as possible
# gunzip file1.gz
decompress
file1.gz
file1.gz
# tar -cvf archive.tar file1 Package file1 into
archive.tar
archive.tar
(-c: create an archive; -v: display the entire process; -f:
use the archive filename, required, and must be the last parameter)
use the archive filename, required, and must be the last parameter)
# tar -cvf archive.tar file1 dir1 Package
file1 and dir1 into archive.tar
file1 and dir1 into archive.tar
# tar -tf archive.tar
Show the contents of an archive
# tar -xvf archive.tar
Extract an archive
# tar -xvf archive.tar -C /tmp Extract the archive to the
/tmp directory
/tmp directory
# zip file1.zip file1
Create a zip-format archive
# zip -r file1.zip file1 dir1 Compress files and directories into a zip-format archive
# unzip file1.zip
Extract a zip-format archive to the current directory
# unzip test.zip -d /tmp/ Extract a zip-format archive to the
/tmp directory
/tmp directory
yum tool:
# yum -y install [package] Download and install an rpm package
# yum localinstall [package.rpm] Install an rpm package, using your own software repository to resolve all dependencies
# yum -y update
Update all rpm packages installed on the current system
# yum update [package] Update an rpm package
# yum remove [package] Remove an rpm package
# yum list
List all packages installed on the current system
# yum search [package] Search for a package in the rpm repository
# yum clean [package]
Clear packages from the cache directory (/var/cache/yum)
# yum clean headers
Delete all header files
# yum clean all
Delete all cached packages and header files
Network:
# ifconfig eth0
Displays the configuration of an Ethernet card
# ifconfig eth0 192.168.1.1 netmask 255.255.255.0
Configure the IP address of the network card
# ifdown eth0
Disable
the ‘eth0’ network device
the ‘eth0’ network device
# ifup eth0
Enable
the ‘eth0’ network device
the ‘eth0’ network device
# iwconfig eth1
Displays the configuration of a wireless network card
# iwlist scan
shows wireless networks
# ip addr show
shows the IP address of the network interface
Other:
# su –
switch to root privileges (different from su)
# shutdown -h now shut down
# shutdown -r now reboot
# top
lists the Linux tasks using the most CPU resources
(press q to quit)
(press q to quit)
# pstree
displays programs in a tree view
# man ping
View the reference manual (for example, for the ping
command)
command)
# passwd
Change password
# df -h
Shows disk usage
# cal -3
Shows the previous month, the current month, and the next month
# cal 10 1988 Shows the calendar for the specified month and year
# date –date ‘1970-01-01 UTC 1427888888
seconds’ Converts a number of seconds relative to 1970-01-01
00:00 into a time
seconds’
00:00 into a time
Ctrl + u Delete the characters from before the cursor to the beginning of the line
Ctrl + k Delete the characters from the cursor to the end of the line
Ctrl + c Cancel the command currently being entered on the line, equivalent to Ctrl
+ Break
+ Break
Ctrl + a Move the cursor to the beginning of the line (ahead
of line), equivalent to the usual Home key
of line), equivalent to the usual Home key
Ctrl + e Move the cursor to the end
of the line
of the line
Ctrl + f Move the cursor forward by one character
Ctrl + b Move the cursor backward by one character
Ctrl + l Clear the screen, equivalent to running the clear command
Ctrl + r Display the : prompt and search related command history based on user input (reverse-i-search)
Ctrl + w Delete from the cursor position back to the beginning of the current word
Ctrl + t Swap the two characters before the cursor
Ctrl + y Paste the last deleted word
Ctrl + Alt + d Show the desktop
Alt + b Move the cursor backward to the previous word
Alt + d Delete from the cursor position to the end of the current word
Alt + F2 Run
Alt + F4 Close the current window
Alt + F9 Minimize the current window
Alt + F10 Maximize the current window
Alt + Tab Switch windows
Alt + hold down the left mouse button Move the window (or scroll the mouse wheel on the bottom taskbar)
[Middle mouse button]
Paste highlighted text. Use the left mouse button to select text. Point the cursor where you want to paste the text. Click the middle mouse button to paste.
Paste highlighted text. Use the left mouse button to select text. Point the cursor where you want to paste the text. Click the middle mouse button to paste.
[Tab] command-line auto-completion. You can use this when working at a shell
prompt. Type the first few characters of a command or filename, then press the
[Tab] key. It will automatically complete the command or show all commands that
match what you typed.
prompt. Type the first few characters of a command or filename, then press the
[Tab] key. It will automatically complete the command or show all commands that
match what you typed.
On the desktop or in the file manager, just press /
to enter a location and open it in the file manager.
to enter a location and open it in the file manager.
Quick search: in vi or Firefox, just press / to enter search mode.
Website links and images can be dragged directly to the desktop or a folder for immediate downloading.
Simply drag a file from the file manager into the terminal to get its full pathname in the terminal.
Click the blank area of the scroll bar with the middle mouse button, and the screen will scroll to that position.