leader

Linux Commands Worth Knowing

View markdown

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

Getting Help


man - help manual.
man ls - get help on the ls command.

Search for Files


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

mkdir - make directory


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.

pwd - print current working directory


pwd - show the current directory you're in.

Change Directories


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.

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.

Look at Files


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

Compare Files


diff - compare two files.
   diff -y - display both files side by side.

Copy, Rename and Delete Files


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.

Update Timestamp


touch - update the files timestamp. if the file doesn't exist then create it.

Split File into Multiple Smaller Files


split - split a large file into pieces
cat  > newfile - Join files together

Change Access Permission


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.

Command History


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

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

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# \ (.*) .*##' filename  - get tne next to last word of each line
   echo "abc def ghi" | sed 's#.* \(.*\) .*##' - get the middle word 'def'
   ls  .php | sed 's#\(.\)#cp  .keep#' > keep.script - create a script that copies every
        php file to filename.keep.

Processes


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

Compress and Archive Files


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

Remote Access


ssh - secure login to a remote computer
scp - secure copy to a remote computer

Run Shell Scripts


source scriptname - run a script

Display to Screen


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 {{ content }} - see what shell you're using.

Environment Variables and Aliases

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

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