More |— Thirty Useful Unix Commands
50 Most Frequently Used UNIX / Linux Commands (With Examples)
15 Practical Grep Command Examples In Linux / UNIX
**ls Sort:** open the last edited file in the current directory ``` vi `ls -t | head -1` ``` To show single entry per line `$ ls -1` Display File Size in Human Readable Format ``` $ ls -lh -rw-r----- 1 ramesh team-dev 8.9M Jun 12 15:27 arch-linux.txt.gz ``` Order Files Based on Last Modified Time ``` $ ls -lt ``` Display Hidden Files `$ ls -a` Display Files Recursively ``` $ ls /etc/sysconfig/networking devices profiles
$ ls -R /etc/sysconfig/networking /etc/sysconfig/networking: devices profiles
/etc/sysconfig/networking/devices:
/etc/sysconfig/networking/profiles: default
/etc/sysconfig/networking/profiles/default:
Visual Classification of Files With Colors
`$ ls --color=auto`
<hr>
Execute a specific command from history
1 service network restart 2 exit 3 id 4 cat /etc/redhat-release
cat /etc/redhat-release Fedora release 9 (Sulphur)
<hr>
### General
`|` - (pipe) to direct the output of a command into another command.
`&&` - operator to execute the next command only if the previous one succeeded.
`cp` - copy a file or directory
|— `cp source dest`
`mv` - move a file
|— `mv source dest`
**Directories**
`pwd` - display the name of your current directory
`ls` - list directory, similar to dir on windows
|— `ls -R dir1` - also lists the contents of any subdirectories dir1 contains.
|— `ls -l` - gives details of file or directory
**File Transfers**
`scp` - secure copy, copies a file over SSH to another server.
|— `scp /local/file [email protected]:/path/to/save/file`
### Files
**Viewing**
`cat` - Display File Contents
`head` - Display first few lines of a file
`tail` - Prints the last few lines of a file, , this is handy for checking log files
**Search**
`grep` - searches files for a specified string or expression
|— `grep motif1 file1` - searches the file file1 for lines containing the pattern motif1
|— `grep motif1 file1 file2 ... filen` - will search the files file1, file2, ... , filen
|— `grep motif1 a*` - will search all the files in the current directory with names beginning with 'a'
|— `grep -c motif1 file1` - will give the number of lines containing _motif1_.
|— `grep -v motif1 file1` - will write out the lines of file1 that do NOT contain motif1.
`fgrep` - Fast Grep, `fgrep failure /var/log/messages`
`find` - lists files and directories recursively on a single line
**Compression**
`gzip` - compress a file
|— `gzip file1` - results in a compressed file called file1.gz, and deletes file1
|— `gzip -v file2` - compresses file2 and gives information on the percentage saved by compression
`gunzip` - restore files to their original state
|— `gunzip file2` - will replace file2.gz with the uncompressed file file2
`tar` -
|— `tar -cf arch.tar * --exclude "*.txt"`- Excluding a set of files from archiving
**Editing**
`vi` - text editor.
|— To edit a file type `vi file`
|— to edit a line press `Esc i`
|— to save changes and exit use `Esc wq`
|— to quit without saving use `Esc q!`
**Diff**
`diff` - display differences between text files
<hr>
### Misc
`date` - display the current date and time
**Fork bomb** - `:(){ :|:& };:` This recursive function is a function that calls itself. It infinitely spawns processes and ends up in a denial of service attack.