## 1. User Environment
Details | commands | Remarks |
---|---|---|
loggedInUser | whoami | |
Current User | id | |
Current directory | Pwd | |
List of files \directories | ls ls -lr ls -ltr |
Details | commands | Remarks |
---|---|---|
System login processes | who -l who -l -H | |
Count all login names and number of users logged on | who -q -H who -H | |
Display the current run level | who -r | |
Display system boot level | who -b | who -b -r |
Display all information | who -a |
-a : Same as -b -d –login -p -r -t -T -u -b Time of last system boot -d Print dead processes -H Print line of column headings -l Print system login processes -m Only hostname and user associated with stdin -p Print active processes spawned by init -q All login names and number of users logged on -r Print current runlevel -t Print last system clock change -T Add user’s message status as +, – or ? -u List users logged in |
## 2. File Management:
Details | Commands | Remarks |
---|---|---|
Create directory | mkdir testdir | |
Copy files | cp ./dir/ ./dir2/ | cp file.doc newfile.doc cp main.cs demo.html lib.vb backup cp main.c demo.h lib.c /home/project/backup/ #Preserve file attributes cp -p filename /path/to/new/location/myfile #Copy all files in backup folder cp * /home//backup #Copy all doc files in backup folder cp *.doc /home/backup #Copy all files recursively in backup folder cp -R * /home/backup #Copy file command with interactive option cp -i foo bar # Verbose output with cp command cp -v file1 file2 -r - recursive -v - verbose |
Copy files & directories recursively | cp ./dir/ ./dir2/ -r | -r - recursive -v - verbose |
Move files | mv ./file1 ./dir1/ | -r - recursive -v - verbose |
Rename files | mv ./file1 ./file2 | -r - recursive -v - verbose |
Move files from one directory to other directory | mv ./dir1 ./dir2 | -r - recursive -v - verbose |
Delete files | rm ./filename rm ./filename1 ./filename2 ./filename3 rm *.pdf | -r - recursive -v - verbose |
Delete directories | rmdir ./dirName/ | -r - recursive -v - verbose -i confirmation |
To view content of file | cat filename | cat file cat /path/file |
To create a file called “test.txt” | cat > test.txt | ##Type following test files and press Ctrl+D at the end of the file This is a line1 in test file. This is a line2 in file. ## press control-D cat test.txt |
To view big files using shell filters (more, less) | cat dwh-log.log | more cat dwh-log.log | less | more dwh-log.log less dwh-log.log |
To combine two or more files | cat test1.txt test2.txt > test-log.txt cat test-log.txt | |
To view file with line numbers | cat -n test1.txt cat -number test1.txt | |
To display TAB characters as ^I | cat -T test.txt | |
To display $ at end of each line, | cat -E test.txt cat --show-ends test.txt | |
Use ^ and M- notation, except for LFD and TAB and show all nonprinting | cat -v test.txt cat --show-nonprinting testfile.txt | |
To show all | cat -A test.txt cat --vET test.txt | |
View all files in directory | cat * cat *.cs | |
joining binary files | cat file1.bin file2.bin file3.bin > large.tar.gz ### extract it tar -zxvf large.tar.gz | |
To show all | cat -A test.txt cat --vET test.txt |
### 2.1 finding lines using cat , sed and awk commands:
Details | Commands | Remarks |
---|---|---|
Read last 100 lines of the file | cat {filename} | tail -100 tail -100f {filename} | Example: cat hugfile.txt| tail -100 tail -100f hugfile.txt |
Read first 100 lines of the file | cat {filename} | head -100 head -100f {filename} | Example: cat hugfile.txt | head -100 head -100f hugfile.txt |
Print \Read nth line of the file using SED (stream editor) | cat {filename} | awk 'NR=={n}' | Example: cat hugefile.txt | awk 'NR==25' |
Print \Read nth line of the file using awk | cat {filename} | sed -n '{n}p' sed -n '{n}p' < {filename} |
Example: cat hugfile.txt | sed -n '100p' sed -n '100p' < hugfile.txt |
Print \Read from mth line to nth lines of the file using SED (stream editor) | cat {filename} | sed -n '{n},{m}p' sed -n '{n},{m}p' < {filename} |
Example: cat hugfile.txt | sed -n '10,50p' sed -n '10,55p' < hugfile.txt |
Print \Read from mth line and nth lines of the file using SED (stream editor) | cat {filename} | sed -n '{m}p;{n}p' sed -n '{m}p;{n}p' < {filename} |
Example: cat hugfile.txt | sed -n '10p;40p' sed -n '10p;40p' < hugfile.txt |
### 2.2 Replace words file using sed (stream editor) command:
Details | Commands | Remarks |
---|---|---|
Replacing or substituting string: Sed command is mostly used to replace the text in a file. | sed 's/{word}/{replacewith}/' {filename} | Example: The below simple sed command replaces the word “data” with “DWH” in the file. sed 's/data/DWH/' hugefile.txt |
Replacing or substituting string: Sed command is mostly used to replace the text in a file. | sed 's/{word}/{replacewith}/' {filename} | Example: The below simple sed command replaces the word “data” with “DWH” in the file. sed 's/data/DWH/' hugefile.txt |
Replacing the nth occurrence of a pattern in a line: Use the /1, /2 etc flags to replace the first, second occurrence of a pattern in a line.: | sed 's/{word}/{replacewith}/{n}' {filename} | Example: The below simple sed command replaces the word “data” with “DWH” in the file for 2nd occurance. sed 's/data/DWH/2' hugefile.txt |
Replacing all the occurrence of the pattern in a line : The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line. | sed 's/{word}/{replacewith}/g' {filename} | Example: The below simple sed command replaces the word “data” with “DWH” in the file fora all occurance. sed 's/data/DWH/g' hugefile.txt |
Replacing from nth occurrence to all occurrences in a line : The substitute flag /{n}g (global replacement) specifies the sed command to replace all the occurrences of the string in the line. | sed 's/{word}/{replacewith}/{n}g' {filename} | Example: The below simple sed command replaces the word “data” with “DWH” in the file for all occurance from nth occurance. sed 's/data/DWH/5g' hugefile.txt |
Replacing from nth occurrence to all occurrences in a line : The substitute flag /{n}g (global replacement) specifies the sed command to replace all the occurrences of the string in the line. | sed 's/{word}/{replacewith}/{n}g' {filename} | Example: The below simple sed command replaces the word “data” with “DWH” in the file for all occurance from nth occurance. sed 's/data/DWH/5g' hugefile.txt |
Parenthesize first character of each word : This sed example prints the first character of every word in parenthesis. | echo "{Tex}" | sed 's/\(\b[A-Z]\)/\(\1\)/g' | Example: echo "Welcome To The Geek Stuff" | sed 's/\(\b[A-Z]\)/\(\1\)/g' |
Replacing string on a specific line number: restrict the sed command to replace the string on a specific line number. | sed '{n} s/{data}/{ReplaceWith}/' {File} | Example: sed '3 s/data/SWH/' hugefile.txt |
Duplicating the replaced line with /p flag: The /p print flag prints the replaced line twice on the terminal. If a line does not have the search pattern and is not replaced, then the /p prints that line only once. | sed 's/{data}/{ReplaceWith}/p' {File.txt} | Example: sed 's/data/SWH/p' hugefile.txt |
Replacing string on a range of lines: can specify a range of line numbers to the sed command for replacing a string. | sed '1,3 s/{data}/{ReplaceWith}' {file} | Example: sed '1,3 s/data/DWH' hugefile.txt |
### 2.3 Deleting lines from a particular file using sed (stream editor) command:
#### 2.3.1 To Delete a particular line say n in this example ```sh # Syntax: sed 'nd' {filename} # Example: sed '5d' hugefile.txt ``` #### 2.3.2 To Delete a last line ```sh # Syntax: sed '$d' {filename} #Example: sed '$d' hugefile.txt ``` #### 2.3.3 To Delete line from range x to y ```sh #Syntax: sed 'x,yd' {filename} #Example: sed '3,6d' hugefile.txt ``` #### 2.3.4 To Delete from nth to last line ```sh #Syntax: sed 'nth,$d' {filename} #Example: sed '12,$d' hugefile.txt ``` #### 2.3.5. To Delete pattern matching line ```sh #Syntax: sed '/pattern/d' {filename} #Example: sed '/data/d' hugefile.txt ```
### 2.3 File Management-vi Editor with Commands: * The vi editor is elaborated as visual editor. It is installed in every Unix system. The vi editor has two modes: **Command Mode**: In command mode, actions are taken on the file. The vi editor starts in command mode.To enter text, you have to be in insert mode, just type `i`
**Insert Mode**: In insert mode, entered text will be inserted into the file. The Esc key will take you to the command mode from insert mode.
By default, the vi editor starts in Command mode. **To save and quit**:
save and quit vi editor from command mode.
Before writing save or quit command you have to press colon (:). Colon allows you to give instructions to vi. exit vi Commands: ------------------ |Commands| Action|Remarks| | :--- | :---: | ---: | |:wq |Save and quit| | |:w |Save| | |:q |Quit| | |:w fname |Save as fname | | |ZZ || Save and quit| | |:q! | Quit discarding changes made | | |:w! |Save (and write to non-writable file) ||
### 2.4 File Management-list files \directories:
Details | commands |
---|---|
List all directories in Unix | ls -l | grep '^d' |
list directories for the current directory | ls -d */ find . -maxdepth 1 -type d |
List of directories for given directory | ls -d /etc/*/ | more |
List only files in current directory: | ls -l | grep -v '^d' |
List all directories in a directory recursively | find . -type d -ls |more find /etc/ -type d -ls |
List all files in a directory recursively | find . -type f -ls find /etc/ -type f -ls | more |
Put above two aliases in your bash shell startup file:> | $ cd $ vi .bash_profile Append two lines: You can create two aliases as follows to list only directories and files. alias lf="ls -l | egrep -v '^d'" alias ldir='ls -d */' #alias ldir="ls -l | egrep '^d'" |
text in files | find . -exec grep -rl "<searchText>" {} \; examples: find . -exec grep -rl "user" {} \; |
###2.5 File Management-How to copy \move files between two unix \linux servers using scp ```sh scp [options] username1@source_host:directory1/filename1 \ username2@destination_host:directory2/filename2 ``` **Details** * The location of the source file is specified by username1@source_host:directory1/filename1, which includes the: * Name of the account on the host computer (username1) * Hostname of the computer on which the source file resides (source_host) * Name of the directory containing the source file (directory1) * Filename of the source file (filename1) * The location to which the source file will be copied is specified by username2@destination_host:directory2/filename2, which includes the: * Name of the account on the destination computer (username2) * Hostname of the computer to which the source file will be copied (destination_host) * Name of the directory to which the source file will be copied (directory2) * Filename of the copy (filename2) **Examples**: ```sh scp ~/sample.txt raz@dev1.aztd.com:~/testdir # It will copy sample.txt from current connected server to testdir folder in dev1 server/ ``` ```sh scp -r raz@dev1.aztd.com:~/dev1-test ~/testdir # it will copy all files in dev1-test directory from dev1 server to testdir directory in connected server recursively. ``` ```sh scp - raz@dev1.aztd.com:"~/dev1-test/*.txt ~/testdir # it will copy all text files in dev1-test directory from dev1 server to testdir directory in connected server. ``` ```sh scp raz@dev1.aztd.com:~/dev1-test/*.pdf raz@dev2.aztd.com:~/dev2-test ~/testdir # it will copy all pdf files in dev1-test directory from dev1 server to testdir directory in dev2 server. ```
###2.6 File Management- Archiving -Z commands
Details | commands | Remarks |
---|---|---|
Uncompressing or UnPacking files | gzip -d test.ods.gz | Display content after uncompress file cat test.ods |less |
Display the contents of a compressed file | zless data.ods.gz zmore data.ods.gz | |
Concatenate compressed files with out using gzip & cat | zcat TestReadme.gz | |
Compare compressed files. | zdiff file1.gz file2.gz zcmp test1.gz test2.gz | |
Search word in Compressed files | zegrep -w '^Test1|Test2' Test.gz
zgrep 'wordToSearch' Test.gz |
zgrep 'Test' /path/to/log/file.gz zgrep 'mail' /var/log/maillog-*.gz zgrep 'error' /var/log/error.log*.gz zgrep --color 'Failed login for' /var/log/secure* |
Search files in a ZIP archive for lines matching a pattern: | zipgrep *.cs Project.zip | |
Search files in a ZIP archive for lines matching a pattern: | zipgrep *.cs Project.zip |
## 3.Networking
### 3.1 Networking-How to check connectivity of another server server ```sh ping <ipAddress> ping <hostName> ping <FQDNForHostname> Example: ping 172.24.541.100 ping aztd1.azure.com ```
###3.2 Networking-How to check connectivity of another server server over particular port using ssh ```sh ssh -p <portNumer> -v <ipAddress> ssh -p <portNumer> -v <hostName> ssh -p <portNumer> -v <FQDNForHostname> Example: ssh -p 9050 172.24.541.100 ssh -p 9040 aztd1.azure.com ```