Linux Beginner's Shell Cheat page

From BITS wiki
(Redirected from Linux Beginner's Cheat page)
Jump to: navigation, search

Here you find a list of useful linux commands. {something} means that you have substitute with a file, command, a location,... Don't type the { or }.

Navigating

pwd prints working directory: shows what dir you're in
ls lists contents - shows the contents of the dir
  • ls -a -> list all contents, including hidden files
  • ls -alh -> list all, in list form and human-readable
cd changes directory: Enter the directory you want to access
  • cd .. -> go to dir one level higher
  • cd ~ -> go to your home dir
  • cd / -> go to the root (/) directory

Managing files and directories

file determines the type of file or directory
  • file Desktop -> Desktop: directory
  • file hello.sh -> hello.sh: POSIX shell script test executable
touch creates an empty file
  • touch test.txt
cat reads the contents of a file (contatenate the contents of several files and output the result)
  • cat test.txt
nano changes the contents of a file with this small text editor
  • nano test.txt
cp copies a file/directory to a new location
  • cp test.txt /tmp
  • cp -R /home /media/FLASH_DRIVE

Help on any Unix/Linux command

man {command} read the manual for the ls command.
  • man ls
man {command} > {filename} Redirect help to a file to download.
  • man ls > manual_of_ls.txt
whatis {command} Give short description of command.
  • whatis ls
apropos {keyword} Search for all Unix commands that match keyword, eg apropos file.
  • apropos unzip


List files in a directory

ls {path} It's ok to combine attributes, eg ls -ahl gets a long listing of all files with types and size in human readable format.
ls {path_1} {path_2} List both {path_1} and {path_2}.
ls -l {path} Long listing, with date, size and permisions.
ls -a {path} Show all files, including important .dot files that don't otherwise show.
ls -h {path} Show the size in human-readable format. "/" = directory, "*" = executable.
ls -F {path} Show type of each file. "/" = directory, "*" = executable.
ls -R {path} Recursive listing, with all subdirs.
ls {path} > {filename} Redirect directory to a file.
ls {path} | more Show listing one screen at a time.


Change to directory

The current directory is represented by ".", the upper by "..", the home by "~".

cd {dirname} There must be a space between.
cd ~ Go back to home directory, useful if you're lost.
cd .. Go back one directory.


Directory manipulations

mkdir {dirname} Make a new directory
rmdir {dirname} Remove a directory. Only works if {dirname} is empty.
rm -r {dirname} Remove all files and subdirs (recursively). Careful! Never ever do something like 'rm -rf /'. This will erase the complete filesystem.
pwd Print working directory. Show where you are as full path. Useful if you're lost or exploring.

File manipulations

cp {file1} {file2} Copy a file or directory
cp -r {dir1} {dir2} Recursive, copy directory and all subdirs.
cat {newfile} >> {oldfile} Append newfile to end of oldfile.
mv {/path/to/oldfile} {/different/path/to/newfile} Moving a file and renaming in one command.
mv {oldname} {newname} Renaming file
rm {file} You can use ? and * wildcards (see below) to apply the command to a group of files at once. "?" is any character; "*" is any string of characters.
ls {file}
rm {file} Good strategy: first list a group to make sure it's what's you think... ...then delete it all at once.


View a text file

more {filename} View file one screen at a time (press space to view following page)
less {filename} Like more, with extra features.
cat {filename} View file, but it scrolls.
more View file one screen at a time.
page {filename} Very handy with ncftp.
nano {filename} Use text editor and don't save.


Edit a text file

pico {filename} One small editor
nano {filename} One very popular easy editor
vi {filename} One very powerful but advanced editor


Create a text file

cat > {filename} Enter your text (multiple lines with enter are ok) and press control-d to save.
nano {filename} Create some text and save it.


Compare two files

diff {file1} {file2} Show the differences.
sdiff {file1} {file2} Show files side by side.


Analysing and mining text files

grep '{pattern}' {file} Find regular expression in file.
  • grep -i -R gi754846 sequences/ -> search recursively and case insensitively for the string gi754846 in the folder sequences/
sort {file1} > {file2} Sort file1 and save as file2.
sort -o {file} {file} Replace file with sorted version.
spell {file} Display misspelled words.
wc {file} Count words in file.

Calculating on the command line

bc -l <<< expression Calculates the expression. Preferred way
  • Tip: put this in .bashrc as an alias, e.g. alias c="bc -l <<<"
bc -l <<< 8+3
expr expression 'expr' evaluate expressions. Does not always what you want, due to rounding expr 8 + 3
$((expression)) Calculates the expression. Does not always what you want, due to rounding
let som="expression" && echo $som Calculates the expression. Does not always what you want, due to rounding let som="8+3/2" && echo $som

Find files on system

find <location> [-name name] [-type {f,d,...}] The -name works with wildcards. Type if f for file (f), d(irectory), l(ink) or others.
alias {name} '{command}' A shortcut to a command. Put a line like 'alias sell="less +G"' in .bashrc.

Wildcards and Shortcuts

* Match any string of characters, eg page* gets page1, page10, and page.txt.
? Match any single character, eg page? gets page1 and page2, but not page10.
[...] Match any characters in a range, eg page[1-3] gets page1, page2, and page3.
~ Short for your home directory, eg cd ~ will take you home, and rm -r ~ will destroy it.
. The current directory.
.. One directory up the tree, eg ls .. (show content of directory up of current location)


Pipes and Redirection

You pipe a command to another command, and redirect it to a file.

{command} > {file} Redirect output to a file, eg ls > list.txt writes directory to file.
{command} >> {file} Append output to an existing file, eg cat update >> archive adds update to end of archive.
{command} < {file} Get input from a file, eg sort < file.txt
{command} < {file1} > {file2} Get input from file1, and write to file2, eg 'sort <old.txt >new.txt' sorts old.txt and saves as new.txt.
{command} | {command} Pipe the output of one command to another that accepts it as input, eg ls | more gets directory listing and sends it to 'more' to show it one page at a time.

Permissions, important and tricky!

At first, root was created. The next step users are created on a system. Next, users can belong to groups, as many they want. To make linux a safe environment, each file can be labeled to allow certain users or groups to read, write and execute it.

Unix permissions concern who can read a file or directory, write to it, and execute it. Permissions are granted or withheld with a magic 3-digit number. The three digits correspond to the owner (you); the group (?); and the world (everyone else).

The digit way to set permissions

You can set permissions to files with a combination of three digit. A digit can have a value between 1 to 7.

Think of each digit as a sum:

  • 1 = execute permission
  • 2 = write permission
  • 3 = write and execute (1+2)
  • 4 = read permission
  • 5 = read and execute (4+1)
  • 6 = read and write (4+2)
  • 7 = read, write and execute (4+2+1) (full access)

Add the number value of the permissions you want to grant each group to make a three digit number, one digit each for the owner, the group, and the world. Here are some useful combinations. Try to figure them out!

  • chmod 600 {filename} You can read and write; the world can't. Good for files.
  • chmod 700 {filename} You can read, write, and execute; the world can't. Good for scripts.
  • chmod 644 {filename} You can read and write; the world can only read. Good for web pages.
  • chmod 755 {filename} You can read, write, and execute; the world can read and execute. Good for programs you want to share, and your public_html directory.

Permissions, another way

You can also change file permissions with characters, one set for the users, one set for the permissions, which are combined with - or +.

User set

  • u = user (yourself)
  • g = group
  • a = everyone (all)

Permission set

  • r = read
  • w = write
  • x = execute

Examples:

  • chmod u+rw {file} Give yourself read and write permission
  • chmod u+x {file} Give yourself execute permission.
  • chmod a+rw {file} Give read and write permission to everyone.
  • chmod o-x {file} Remove execute permission to the world

System info

date Show date and time.
df Check system disk capacity.
du Check your disk usage and show bytes in each directory.
more /etc/motd Read message of the day, "motd" is a useful alias..
printenv Show all environmental variables (in C-shell% - use set in Korn shell$).
quota -v Check your total disk use.
uptime Find out system load.
w Who's online and what are they doing?

Unix Directory Format

Long listings (ls -l) have this format:

       - file
       d directory,                                            * executable
       ^   symbolic links (?)  file size (bytes)   file name   / directory
       ^           ^               ^                  ^        ^
       drwxr-xr-x 11 mkummel      2560 Mar  7 23:25 public_html/
       -rw-r--r--  1 mkummel     10297 Mar  8 23:42 index.html
                                               ^
        ^^^        user permission  (rwx)      date and time last modified
           ^^^     group permission (rwx)
              ^^^  world permission (rwx)

How to Make an Alias

An alias is a shorthand for a command. In other words, an alias lets you type something simple, and do something complex. For example, you want to type "dir" instead of "ls -l". For this, add to your .bashrc the line alias dir='ls -l'. Type 'nano .bashrc' in your home directory and look for the alias section and add your line. When you have saved your alias, read in the .bashrc file again by typing . ~/.bashrc. Note: an alias with the name of a Unix command will make that command unavailable.

Here are a few aliases from my .bashrc file:

  1. enter your aliases here in the form:
  2. alias this = means this

alias c='bc -l <<<' alias dirsize='du -sh */' alias echo='echo -e' alias hosts='cat /etc/hosts' alias ll='ls -lh' alias mypubip='curl corz.org/ip && echo '\\n'\' alias sell='less +G' alias sshy='ssh -Y' alias turnpdf='pdftk in.pdf cat 1-endS output out.pdf' alias xclip='xclip -selection clipboard' alias biocLite='echo '\source("http://bioconductor.org/biocLite.R")'\'

How to Make a Script

A bash script is a text file with commands listed in it, which can be executed. Here's a useful example.

We will make a short script to rename a bunch of file from htm to html. Make a text file (eg with nano): $ nano ~/bin/htm2html. Enter the content below. The first line (the shebang line) is special: it tells Unix what program or shell should execute the script. Other # lines are comments.

       #! /bin/bash
       # htm2html converts *.htm files to *.html
       foreach f ( *.htm )
         set base=`basename $f .htm`
         mv $f $base.html
       end

Save this in your ~/bin folder (scripts in here are automatically available at your command line, i.e. it is in your PATH). Set the right execute permissions to be able to run the script: $chmod +x ~/bin/htm2html. Change to a directory with .htm files and type htm2html, and it will do its stuff.

Think about scripts whenever you find yourself doing the same tedious thing over and over.

Dotfiles (aka Hidden Files)

Dotfile names begin with a "." These files and directories don't show up when you list a directory unless you use the -a option, so they are also called hidden files. Type ls -la in your home directory to see what you have.

Some of these dotfiles are crucial. They initialize your shell and the programs you use, like autoexec.bat in DOS and .ini files in Windows. rc means "run commands". These are all text files that can be edited, but change them at your peril. Make backups first!

Here's some of what I get when I type ls -laF:

   .bashrc 	my Bash-shell startup info, important!
   .bash_history 	history of all command I have typed
   .Rhistory 	my R commands history
   .profile 	Bash shell startup info, important!

Comparison DOS and UNIX commands

Action DOS UNIX
change directory cd cd
change file protection attrib chmod
compare files comp diff
copy file copy cp
delete file del rm
delete directory rd rmdir
directory list dir ls
edit a file edit pico
environment set printenv
find string in file find grep
help help man
make directory md mkdir
move file move mv
rename file ren v
show date and time date, time date
show disk space chkdsk df
show file type cat
sort data sort sort