---
title: Linux Commands
h1: Linux Commands Worth Knowing
description: Here are Linux commands that I use.
author: JWRR
date: January 11, 2019
theme: a
...
[View markdown](linux-commands.md)
{{ toc }}

I went through my history buffer and these are the commands I use.

<a name="man"></a>
## Getting Help (man)
```
man - help manual.
man ls - get help on the ls command.
```

<a name="ls"></name>
## Search for Files (ls, find)
```
ls - list the files in the current directory
ls -l - use the long format that provides more information such as file size and date.
ls -ltr - sort the file list by (t)ime and in (r)everse order so the newest file is last.
ls *txt - return just the files that end in txt. The '*' is a multicharacter wildcard
ls a.b - the '.' is a single character wildcard.
ls -a - show all the files (even the hidden files that start with '.')
ls -d * - -d returns just the directories.
ls -F -  append a symbol to the end of the filename that indicates the filetype */=>
ls -s realpath shortcut
find . - returns all the files in current directory and all sub-directories
```

<a name="mkdir"></a>
## mkdir - make directory (create folder)
```
mkdir abc - make directory abc
mkdir -p a/b/c - make directory a, and then make sub-directory b in a, and make c in b.
```
<a name="pwd"></a>
## pwd - print current working directory ($CWD)
```
pwd - show the current directory you're in.
```

<a name="cd"></a>
## Change Directories (cd, pushd, pop)
```
cd - change your home directory
cd ~ - change to your home directory (same as cd)
cd ~/abc - change to sub-directory abc that is in your home directory
cd ~abc - change to user abc's home directory
cd .. - go up one directory. cd ../.. goes up to directories
cd / - go to the top root directory
cd abc - go to sub-directory abc
```

pushd and pop work together.  First use pushd to change to a directory.  Then
use pop to quick return to the previous directory.  You can pushd multiple
times and then pop to return the the directories.  I rarely pushd more than
once or twice before using the pop command.

```
pushd /usr/local/bin - go to /usr/local/bin (just like cd)
popd  - return back to directory you were in when you entered pushd.
        you can stack multiple pushd's.
```

<a name="df"></a>
## Admin Commands
```
df - show the free and used diskspace on every disk in the system
df . - show the free and used diskspace of the current disk
```

```
du . - show the diskspace used by the current directory and all it's sub-directories
du -sch * - show just the (s)ummary of the size of each folder (and file) defined by
            the * wildcard and show a (c)grand-total, and print the sizes in human form
            (K, M, G suffix).  This is useful if someone is hogging the disk, but you
            don't know who. Just go to the home account (cd ~/..) and run du -sch.
```

<a name="cat"></a>
## Look at Files (cat, more, less, head, tail)

```
cat - show the file
more - display a file, one page at a time
less - a more advanced version of more
head - show the first lines of a file
tail - show the last lines of a file
```
<a name="diff"></a>
## Compare Files (diff)
```
diff - compare two files.
   diff -y - display both files side by side.
```


<a name="mv"></a>
## Copy, Rename and Delete Files (cp, mv, rm)
```
mv a b- rename a to b
```

```
cp - copy a to b
cp -rf a b - copy directory a to directory b. r=recursive, f=force (the f is probably
             not needed by I always use it)
```

```
rm a - delete file a
rm -rf a - delete directory a
rm -rf /* - delete everything
\\rm filename - sometimes a overly cautious sysadmin redefines the rm command to 
    prompt you to confirm every deletion. Thanks but no thanks. Put '\' in front for rm
    and the rm command as the Unix masters intended.
```
<a name="touch"></a>
## Update Timestamp (touch)
```
touch - update the files timestamp. if the file doesn't exist then create it.
```

<a name="split">
## Split File into Multiple Smaller Files (divide)
```
split - split a large file into pieces
cat * > newfile - Join files together
```

<a name="chmod"></a>
## Change Access Permission (chmod, chgrp)
```
chmod - change access privileges
   chmod 777 filenames - let every one have full access
   chmod 644 filenames - owner can read and write, group and world can just read
   chmod 755 filenamems - owner can read/write/execute, group and world can just read/execute
   chmod -R 644 - change privileges recursively for all sub-directories
chgrp - change the group that the file is in.
```

<a name="history"></a>
## Command History (show history, repeat repeat previous commands)
```
history - get list of your most recent commands
!ls - execute the most recent ls command
!42 - execute command number 42 in history's command list
up-arrow, down-arrow
tab ahead
```

<a name="grep"></a>
## grep - search multiple files for string
```
grep - search through a list of files for a string.
grep -i abc \* - case insensitive search. search for abc in all files of the directory
grep -v abc \* - return lines that don't have the string 'abc'
grep -q abc * - return just the filename if the file contains the search string
```

<a name="sed"></a>
## sed - file and replace in multiple files
```
sed - Stream Editor
   sed 's#from#to#g' filename > newfilename - replace all occurrences of 'from' with 'to'
   sed 's#^#abc#' - add 'abc' at start of every line
   sed 's#$#zyz#' - add 'xyz' at end of every line
   sed 's#.* ##' - leave just one word, the last word, on each line
   sed 's# .*##' - leave just one word, the first word, on each line
   sed 's# *$## - remove trailing spaces at end of line
   sed 's# *\ (.*) .*#\1#' filename  - get tne next to last word of each line
   echo "abc def ghi" | sed 's#.* \(.*\) .*#\1#' - get the middle word 'def'
   ls  *.php | sed 's#\(.*\)#cp \1 \1.keep#' > keep.script - create a script that copies every
        php file to filename.keep.
```

<a name="process"></a>
## Processes (ps, top, bg, fg, kill, exit)

```
ps - show the processes that are running
top - show the processes that are using the most cpu.
kill - stop a process
gvim& - '&' starts a process in background.
Ctrl-z bg - Ctrl-z suspends a program. bg starts it running in the background.
Ctrl-z fg - fg starts it running in the foreground.
exit - close the shell
```

<a name="compress"/></a>
## Compress and Archive Files (zip, gzip, bzip, tar)
```
zip, unzip - compress a file, uncompress a file
gzip, gunzip - compress a file, uncompress a file. Better than zip
bzip, bunzip - compress a file, uncompress a file. Better but slower than gzip
tar zcvf filename.tgz subdirectoryname - compress and save a directory.
tar zxvf filename.tgz - uncompress and restore the saved contents to the the directory
```

<a name="ssh"></a>
## Remote Access (ssh, scp)
```
ssh - secure login to a remote computer
scp - secure copy to a remote computer
```

<a name="source"></a>
## Run Shell Scripts
```
source scriptname - run a script
```

<a name="echo"></a>
## Display to Screen (echo)
```
echo "abc def" - display the string on the screen. Usually used in a script.
echo "abc" > filename - create a file with one line, "abc"
echo "def" >> filename - append "def" to the end of the file.
echo $HOSTNAME - see what an environment variable is set to.
echo $0 - see what shell you're using.
```

<a name="env"></a>
## Environment Variables and Aliases (env, alias, set, setenv)
env - list all the system environment variables
env |grep HOSTNAME - see what variable HOSTNAME is set to
alias h "history" - create keyboard shortcut. now the history command is run you type h.
alias hg "history |grep " - search history. 
      example> hg cd  - this returns the recent 'cd' commands

<a name="thereset"></a>
## More
```
date - show the dae
wc - show the line count, word count and character count. 
enscript -f Courier7 -C -DDuplex:true -P@printer-name-or-ip filetoprint.txt - print to postscript printed
```


