helps‎ > ‎

basic bash

Helpful shell (terminal) commands


package handling (getting info on available or installed)

check dependencies
    apt-get check [package]

simulate install
    apt-get install -s [package]

attempt to install dependencies
    sudo apt-get build-dep [package]

install package
    apt-get install [package]

install without broken dependencies
    apt-get install -f [package]

reinstall package
    apt-get install --reinstall [package]

remove package
    apt-get remove [package]

remove package including its config files
apt-get remove --purge [package]

clean cache
    apt-get clean

clean cache from packages that can't be downloaded
    apt-get autoclean

clean broken install
    apt-get install -f

or, failing that
    sudo dpkg --configure -a 

search for packages in the apt cache with a name, or part of a name
    apt-cache search [package]

to list dependencies of installed
    apt-cache show [package] 

to list dependencies of package not installed
    apt-cache search [package] 

list any installed packages with a name
    dpkg --list | grep <name>

check version of installed package
    dpkg -s [package] 

also can use dpkg-query, wildcards too
    dpkg-query -l [package]

to find the package that owns a command
    dpkg -S [command]

full upgrade of the system
    see my upgrade Debian page

install a package from a tar ball
(check the Readme or INSTALL file in unpacked source tarball)
1. straight install
    sudo make install

2. create a .deb file
    sudo checkinstall

-to make the .deb without install:
    sudo checkinstall --install=no

use apt-file
    apt-file update
find package that installs a missing file
    apt-file search [file name]

terminal shortcut keys
home  moves the cursor to the beginning of the command line,  
end moves the cursor to the end of the command line.  
Ctrl-W deletes the word immediately before the cursor,  
Ctrl-K deletes everything immediately after the cursor. 
You can undo a deletion with Ctrl-Y.
Ctrl-C cancels the active process
up key cycles through previously made commands
tab auto completes if there's only one possibility
Shift-pg up/home move up the terminal (handy with no scroll bar)/go to top
Shift-pg dn/end move down the termnal/go back to the prompt (down does too)

very useful in a terminal:
Ctrl-R shows reverse history, type a few letters to search a previous line, then press Ctrl-R repeatedly to find the command, and hit Enter on the one you want.

bash commands n tips

what user is logged in?

force shutdown
    sudo shutdown -h now

planned shutdown
    sudo shutdown -h 01:30 &

cancel planned shutdown
    sudo shutdown -c

    sudo logout

    sudo reboot

!delete a dir and its contents entirely !!!
    rm -rf /some-dir/some-dir/

get a list of local partitions
    sudo fdisk -l

get the uuid of all partitions
    ls -l /dev/disk/by-uuid

get memory usage stats

change a user password
    sudo passwd <username>

change the root password
    sudo passwd

Unix commands

man [command]                shows man page
apropos [command]          shows what a command does = man -k [command]
man [comm.] > [comm.].manpage              to feed the manpage to a file
w                                     find users logged in and their sctivity
cal                                   show simple calendar
cal [year]                          get full year calendar
cal 10 2005                      shows Oct, 2005
ls                                     lists dir contents
   -s                                  list total files and no. files in sub dirs
   -sd                                lists total no of files only
   -s [dir]                           lists contents of [dir] and the no of files in its sub dirs
   -t                                   list most recently accessed files first
   -R                                  lists files recursively
   -l                                   long list files in a dir
   -m                                 list comma separated
   -x                                  list by rows
   -r                                   list in reverse order
   --color=auto --group-directories-first                 list dir/files horizontally
   -lh --color=auto --group-directories-first            shows dir/file list with permissions,                                                                                 owner, file size, date modified
   -alhrt                        shows dir/file list sorted recently modified first
   -alh                    shows dir/file list including hidden
   -d .* --color=auto           shows only hidden files
   -lhd .* --color=auto
   -Rh                                shows files recursively
cp {file} {newfile}  copies file with new name
    -u                                 update switch copies only if source file newer than dest file
mv {file} {dir}/                 moves file to another dir
mv {file} {newfile}  changes the file's name (moves to new file, deletes old)
mkdir -p                           creates dir with necessary parent dir's
du                                    disk usage of all dir below location
    -s                                 total disk usage only
    -a                                 include size of all files
df                                     disk free space on system
touch {file}                       creates the file, or modifies date-time to current on file
compress {file}   compresses file to .z
uncompress {file.z}   uncompresses file.z
rm -i [file]                         interactive remove
     -r [dir]                         recursive removal of a dir
     -v [file]                        verbose -explains what is being done
file [file] [dir]   shows what type a file or dir is
head -10 [file] view 1st 10 lines of file
head -5 [file] [file]   show for multiple files
tail -10 [file]   show last 10 lines of file
head -50 [file] | tail -10   shows the lines 41-50 of file (using a pipe)
cat [file]                            displays file content (entire)
more [file]                        displays file content part-by-part
       -cs [file]                     -c display clears screen each new page and -s 
                                        suppresses blank lines
more +20 [file]         displays from line 20
more +/[text] [file]   displays from certain text in file
        more controls:
{n}Enter    scroll n lines down
d       scroll down half a page
b  scroll back half page
h  list commands in more program
<                                      use this to pass input file to the command
>                                      use to send output of command to a file
>>                                    use to append output to a file
wc [file]                            shows no. of lines, words, characters in a file
wc -l                                 counts only lines...
[command] | wc -l  now wc counts the lines output from the command     
                                                and gives you a number (command e.g. ls, who)
cat -n [file]                        shows file with line numbers
nl [file]                             does the same
    -ba [file]                       numbers blank lines
    -s', ' [file]                      changes default tab after number to comma and space
echo boo                          writes "boo" to screen
echo boo >> [file]  appends "boo" to a file 
echo *                               list all files in the dir
echo b*                             list all the file in dir beginning with b
?                                       single character wildcard (* can be any length of characters)
ls [a-z]*                             lists all file/dir beginning with a letter
grep [word] [file]  display lines from a file with a certain word or text
grep 'ho*' [file] displays lines containing "ho" in any word
-i  ignores case of search criterea
-n  numbers the lines that are matched
-c  count of matching lines
-l  lists files with matching lines
sed -e 's/cap/hat/g' [file] or                     sed substitutes cap with hat in a file...
grep 'cap' [file] | sed -e 's/cap/hat/g'    (g tells it to do every ocurrance) 
sed -e 's/cap/hat/g;s/cat/rat/g'                    sed carries out 2 substitutions

UNIX in 24 hours
 - by Dave Taylor. James C. Armstrong, Jr.
I recommend going through this book if you are new to Linux, or perhaps not so new but haven't read up properly about the basic functions of the UNIX OS.

store script files in ~/scripts
to be able to run your scripts just as bash commands (instead of sh ~/scripts/myscript)
export PATH=$PATH:$HOME/scripts

to ~/.bashrc at the end

more technical

move files found in a list of file names (files.list)
while read file ; do mv "$file" /dir/dir/moved/ ; done < files.list

replace all text occurring in all files under the current dir
find /abc/abc -type f | xargs perl -pi -e '/xyz/xyz'
(note, I replaced a directory path found in the files)

in a specific file
sed -i 's#old#new#gi' **/*.suffix

change file names
find . -depth -name "* *" -execdir prename -n 'y/oldname/newname/' {} +

using vi