Skip to content

Instantly share code, notes, and snippets.

@r-sal
Last active December 13, 2015 18:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r-sal/4953628 to your computer and use it in GitHub Desktop.
Save r-sal/4953628 to your computer and use it in GitHub Desktop.
Most Important UNIX commands

Essential UNIX commands

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   

history | more

1 service network restart 2 exit 3 id 4 cat /etc/redhat-release

!4

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
&nbsp;&nbsp;|&#8212; `cp source dest`  
`mv` - move a file
&nbsp;&nbsp;|&#8212; `mv source dest`  
  
  
**Directories**  
`pwd` - display the name of your current directory  
`ls` - list directory, similar to dir on windows  
&nbsp;&nbsp;|&#8212; `ls -R dir1` - also lists the contents of any subdirectories dir1 contains.  
&nbsp;&nbsp;|&#8212; `ls -l` - gives details of file or directory  
  
**File Transfers**
`scp` - secure copy, copies a file over SSH to another server.  
&nbsp;&nbsp;|&#8212; `scp /local/file user@host.com:/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  
&nbsp;&nbsp;|&#8212; `grep motif1 file1` - searches the file file1 for lines containing the pattern motif1  
&nbsp;&nbsp;|&#8212; `grep motif1 file1 file2 ... filen` - will search the files file1, file2, ... , filen  
&nbsp;&nbsp;|&#8212; `grep motif1 a*` - will search all the files in the current directory with names beginning with 'a'  
&nbsp;&nbsp;|&#8212; `grep -c motif1 file1` - will give the number of lines containing _motif1_.  
&nbsp;&nbsp;|&#8212; `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  
&nbsp;&nbsp;|&#8212; `gzip file1` - results in a compressed file called file1.gz, and deletes file1  
&nbsp;&nbsp;|&#8212; `gzip -v file2` - compresses file2 and gives information on the percentage saved by compression  
  
`gunzip` - restore files to their original state  
&nbsp;&nbsp;|&#8212; `gunzip file2` - will replace file2.gz with the uncompressed file file2  
  
`tar` -  
&nbsp;&nbsp;|&#8212; `tar -cf arch.tar * --exclude "*.txt"`- Excluding a set of files from archiving  


**Editing**  
`vi` - text editor. 
&nbsp;&nbsp;|&#8212;  To edit a file type `vi file`  
&nbsp;&nbsp;|&#8212;  to edit a line press `Esc i`  
&nbsp;&nbsp;|&#8212;  to save changes and exit use `Esc wq`  
&nbsp;&nbsp;|&#8212;  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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment