-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractical Linux Commands
More file actions
194 lines (168 loc) · 11.9 KB
/
Practical Linux Commands
File metadata and controls
194 lines (168 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
Practical Linux Commands
Basic shell Commands
-------------------------------------------------------------------------
Command : Syntax Examples [ Explanation ]
-------------------------------------------------------------------------
cd : cd foo [ change directory ]
ls : ls -lt [ list files witrh time modified and detail ]
ls -i : ls -i [ Show inode details ]
cp : cp foo.sh ~/folder/foo.sh [ copy source to destination ]
mv : mv foo.sh ~/folder/foo.sh [ move source to destination ]
rm : rm filename [ remove file , -rf recursively no ask ]
wc : wc filename [ word count, -l, -w, -c ]
cal : cal [ shows calendar ]
cat : cat file [ show file contents ]
tee : cat file1 | tee file 2 [ tee pipeline ]
more : cat file | more [ file perusal filter for screen viewing ]
less : cat file | less [ opposite of more ]
sort : sort data.txt [ sort contents of a file or files, used with uniq ]
touch : touch filename [ change file timestamps or create an empty file ]
file : file filename [ determine the filetype ]
mkdir : mkdir directory [ create a directory ]
rmdir : rmdir directory [ remove an empty directory ]
type : type man [ shows info of a shell command ]
which : which ls [ locate the folder of a command executable ]
whereis : whereis ls [ locate the folder of a command executable + the man pages ]
printenv : printenv [ Print all of part of the environment variables ]
head : head -n 5 [ Shows the first 5 lines from the beggining of a file ]
tail : tail -n 5 [ Shows the last 5 lines from the end of a file ]
date : date [ Shows the current time and date ]
who : who [ Shows who is logged on
whoami : whoami [ Print the effective userid ]
lastlog : lastlog [ reports the most recent login of all users or of a given user ]
id : id [ Print real and effective user and group IDs ]
ln : ln -s Target link_name [ make symbolic links between files ]
screen : screen [ screen manager with VT100/ANSI terminal emulation ]
Environment Variables Check
-------------------------------------------------------------------------
Command : [ Explanation ]
-------------------------------------------------------------------------
echo $$ : [ Prints the process id of the running program ]
echo $0 : [ Returns the name of the running process/program ]
echo $@ : [ Returns the values of the parameters given to a command ]
echo $# : [ Returns the number of parameters isued to a command ]
echo $1 : [ Returns the 1st argument provided in a shell script ]
echo $? : [ Return the exit code of the last command 0:Succesfull/1:Unsuccesful ]
printenv SHELL : [ Print the environment variable named SHELL ]
export : [ Export a variable to a forked process ]
Disk Status
-------------------------------------------------------------------------
Command : [ Explanation ]
-------------------------------------------------------------------------
du -sch .[!.]* * |sort -h : Disk Usage of Hidden Files and Folders
Searching & Editing
-------------------------------------------------------------------------
find Command : [ Explanation ]
-------------------------------------------------------------------------
find Command
find . -user bob : [ Find all the files belonging to a specific user ie. bob ]
find /var/www -group developer : [ Search files belong to a specific group ie. developer ]
find / -mtime 50 : [ Find files modified 50 days back ]
find / -atime 50 : [ Find files accessed in the last 50 days ]
find / -mmin -60 : [ Find files modified in the last hour ]
find / -amin -60 : [ Find accessed files in last 1 hour ]
find / -size 50M : [ Find all 50M Files ]
find / -size +50M -size -100M : [ Find all files between 50 and 100MBytes ]
find /tmp -type f -empty : [ Find all empty files ]
find ~/ -type d -empty : [ Find all empty directories ]
find /var/log -mmin 10 : [ Find the logs which have been modified within the last 10 minutes ]
find . -type f -exec ls -sh {} \;|sort -n -r | head -5 : [ Display the 5 largest files in the directory and subdirectories ]
find / -perm /4000 2>/dev/null : [ Find all files having the SetUID bit set ]
find / -perm /2000 2>/dev/null : [ Find all files having the SetGID bit set ]
find / -perm /1000 2>/dev/null : [ Find all files having the sticky bit set ]
Searching & Editing
-------------------------------------------------------------------------
grep Command : [ Explanation ]
-------------------------------------------------------------------------
grep "literal string" filename : [ Search for the string in the file ]
grep "this" demo* : [ Search pattern in multiple files ]
grep -i "string" * : [ Case insensitive grep of a string in multiple files ]
grep -A3 -i "example" : [ Display 3 lines after match ]
grep -B3 "example" FILENAME : [ Display 3 lines before match ]
grep -C2 "test" FILENAME : [ Display 2 lines around match ]
grep -v "go" FILENAME : [ Invert Match ]
grep -c "go" FILENAME : [ Count the Number of the matches ]
grep -l "test" FILES* : [ Display only filenames with matches ]
grep -o "is.*line" FILENAME : [ Show only the matched String ]
grep -n "go" FILENAME : [ Show line numbers with Output ]
grep -irl bob /var/log : [ list all the log files containing the user bob ]
grep -E '^#' /etc/config.file : [ Use Regular Expression and print the lines with comments of a given file ]
grep -Ev '^#|^$' /etc/config.file : [ Print all the lines of a conf file instead of the empty and the comments ]
Text Manipulation
-------------------------------------------------------------------------
Command : [ Explanation ]
-------------------------------------------------------------------------
echo ${variable #?} : [ Extract all but the 1st character ]
echo $variable | cut -c1 : [ Extract the 1st Character ]
echo ${variable:0:1} : [ Extract the 1st Character ]
echo ${variable%${variable#?}} : [ Extract the 1st Character ]
sed 's/Nick/John/g' report.txt : [ Report every occurence of Nick with John ]
sed -n12,18p file.txt : [ Show only lines 12-18 of file.txt ]
sed 12,18d file.txt : [ Show all of file.txt except for lines form 12 to 18 ]
sed G file.txt : [ Double space file ]
sed '$d' file.txt : [ Delete the last line ]
sed '/boom/!s/aaa/bb/' : [ Unless boom is found replace aaa with bb ]
sed 's/.$//' file.txt : [ A way to replace dos2unix ]
sed 's/^[ ^t]*//' file.txt : [ Delete all spaces in front of every line ]
sed '/./!d' file.txt : [ Delete all blank lines from a file ]
sed 's/.\x08//g' file : [ Remove nroff overstrikes ]
sed -e :a -e 's/<[^>]*>//g;/</N;//ba' : [ Remove HTML tags ]
sed 's/[^[:digit:]]//g' : [ Discard everything except integers ]
sed 's/[^[:uppper:],.]//g' : [ Keep only an upper Character and Comma ]
sed '/^#\|^$/d' filename : [ Show all the lines of a file instead of the commented and empty ]
Monitor Performance/Resources
-------------------------------------------------------------------------
Command : [ Explanation ]
-------------------------------------------------------------------------
cat /proc/cpuinfo : [ Shows CPU Info ]
free : [ Shows Free Memory ]
ps -eo pid, euser,ruser,comm : [ Process ID, Effective, Real UID ]
ps m -o pid,tid,command : [ Print the Thread IDs ]
top : [ Display the linux processes ]
htop : [ An interactive process viewer ]
iostat : [ System Input and Output Storage Device Statistics ]
lsscsi : [ Show info about the ssd drivers, sata drives etc. ]
sudo blkid : [ Print block devices information ]
cat /proc/cmdline : [ Show the Bootloader Options ]
strace -e trace=network,read,write executable args : [ Monitor system calls from a user program ]
Bonus - Kill all the processes of the dd command one by one:
ps -C dd | grep -v "PID" | awk '{ print $1'} | while read CMD;do kill $CMD; done
Network Tools
-------------------------------------------------------------------------
Command : [ Explanation ]
-------------------------------------------------------------------------
lsof -i : [ List Programs currently used or listening to ports ]
dig +short myip.opendns.com @resolver1.opendns.com : [ Return WAN IP ]
tcpdump -i eth1 -w -lpv -bert > /dev/null : [ Measure the Network throughput with tcpdump ]
iptraf : [ Opensource real time network monitor utility ]
sudo nethogs eth1 : [ Net Motinor & Real time Network traffic bandwidth ]
whois domain.com : [ Client for the whois directory service ]
host -t ns domain.com : [ Shows the DNS Servers for the domain ]
host -l domain.com ns2.zoneedit.com : [ Perform DNS zone transfer ]
arp : [ Manipulate the system ARP cache ]
sudo netstat -tupln : [ Show the Tcp and Udp listing ports with PID ]
ssh -L 9000:test.com:80 sshserver -pxx -N : [ Perform ssh tunneling of localhost port 9000 to remote port 80 ]
nc [ip] [port] (issue --> GET / HTTP/1.1) : [ Netcat client mode - Connect to a specific ip:port ]
nc -l 3333 : [ Create a Server and set it to listen to port 3333 ]
nslookup : [ Featch mail exchanger servers ]
> set type=mx
> bulbsecurity
nmap
-------------------------------------------------------------------------
Command : [ Explanation ]
-------------------------------------------------------------------------
nmap -sC -sV [ip] : [ Scan and retrieve software version & use NSE ]
nmap -SA 192.168.0.1 : [ Scan a host to detect a firewall ]
nmap -SP 192.168.0.* : [ Find out live hosts in class C IP range ]
nmap -sP 192.168.0.1/24 : [ Find out live hosts in class C IP range ]
Syncing - Backup
-------------------------------------------------------------------------
Command : [ Explanation ]
-------------------------------------------------------------------------
wget -r -nH -nd -np -R index.html* http://test.com/videos : [ Download all files in a directory without the parent structure ]
tar cBvf - directory | ssh remote_host tar xBvpf : [ Compress a directory and send it to a remote host where it uncompresses itself ]
dd if=/dev/hdd5 | gzip -9 | nc -l 3333 : [ Server Machine to perform backup ]
nc 192.168.0.1 3333 | pv -b > hdd5partion.img.gz : [ Connect to the above server and receive the partition image ]
rsync -rsh='ssh -p 3333' -azv bash hostname:~/tmp . : [ Use a different port than 22 for rsync, copy all bash contents recursively, verbose mode and compress ]
rsync -avz -e "ssh -p $portNumber" user@remoteip:/path/to/files/ /local/path/
rsync -n : [ Can be used in case you want a dry run, not dry rum :-) ]