Difference between revisions of "Basic linux commands everyone should know!"

From CUC3
Jump to navigation Jump to search
import>Csw34
import>Cen1001
m (Formatting change)
 
(10 intermediate revisions by 2 users not shown)
Line 3: Line 3:
 
You may want to read the computer office's [http://www-co.ch.cam.ac.uk/software/unix/booklet/ introduction to Unix booklet] .
 
You may want to read the computer office's [http://www-co.ch.cam.ac.uk/software/unix/booklet/ introduction to Unix booklet] .
   
A really useful guide to [http://www.tldp.org/LDP/abs/html/ bash scripting].
+
Also, here is a really useful guide to [http://www.tldp.org/LDP/abs/html/ bash scripting].
   
  +
'''NOTE''': You can always look up a command using ''man
* ''cd''
 
  +
man ls
  +
This will display info on the options associated with the ''ls'' command. To exit the info page, press ''q''.
  +
  +
==ls==
  +
ls
  +
This lists the contents of the current directory
  +
ls -lrth
  +
This will list the contents of the current directory showing more information and sorting them so the most recently modified file will be displayed last. The ''h'' flag makes the file sizes more human friendly!
  +
ls -a
  +
This will list '''a'''ll files i.e. the normal ''ls'' command will not display files beginning with a ''.'' i.e. ''.bashrc''
  +
  +
== cd ==
 
The ''''c'''hange '''d'''irectory' command
 
The ''''c'''hange '''d'''irectory' command
 
cd
 
cd
 
cd ~
 
cd ~
cd /home/user
+
cd /home/username
 
These all take you to your home directory
 
These all take you to your home directory
 
cd ../..
 
cd ../..
Line 16: Line 28:
 
This takes you into the directory called ''newdir''
 
This takes you into the directory called ''newdir''
 
cd ~/Documents
 
cd ~/Documents
This takes you into you Documents folder (actually located at ''/home/user/Documents'')
+
This takes you into you Documents folder (actually located at ''/home/username/Documents'')
 
cd ~csw34
 
cd ~csw34
 
This takes you into the home directory of user ''csw34''
 
This takes you into the home directory of user ''csw34''
cd /sharedscratch/user
+
cd /sharedscratch/username
 
This takes you to your ''sharedscratch'' directory (only on clusters)
 
This takes you to your ''sharedscratch'' directory (only on clusters)
   
* ''cp''
+
== cp ==
 
cp file1 file2 newdir/
 
cp file1 file2 newdir/
 
Copies ''file1'' and ''file2'' to the directory ''newdir''
 
Copies ''file1'' and ''file2'' to the directory ''newdir''
Line 28: Line 40:
 
Recursively copies the directory ''newdir'' and all its subdirectories to ''csw34'''s home directory.
 
Recursively copies the directory ''newdir'' and all its subdirectories to ''csw34'''s home directory.
   
'''WARNING''': you can overwrite files using ''cp'' i.e. if in the first example ''file2'' already exists, you will replace its contents with those of ''file1''!
+
'''WARNING''': you can overwrite files using ''cp'' i.e. if in the first example ''file2'' already exists, you will replace its contents with those of ''file1''!
   
  +
For information on copying files between machines see the guide for ''scp'' linked from the [[Wales Group]] page.
* ''mv''
 
  +
  +
== mv ==
 
mv oldfilename newfilename
 
mv oldfilename newfilename
This effectively renames the file 'oldfilename'' to ''newfilename''
+
This effectively renames the file ''oldfilename'' to ''newfilename''
 
mv test ../
 
mv test ../
This moves the file ''test'' one level up the directory tree.
+
This '''m'''o'''v'''es the file ''test'' one level up the directory tree.
   
 
'''WARNING''': you can overwrite files using ''mv'' just like when you use ''cp'' so be careful!
 
'''WARNING''': you can overwrite files using ''mv'' just like when you use ''cp'' so be careful!
   
* ''mkdir''
+
== mkdir ==
 
mkdir newdir newdir2
 
mkdir newdir newdir2
 
This will create two new directories, ''newdir'' and ''newdir2''
 
This will create two new directories, ''newdir'' and ''newdir2''
Line 44: Line 58:
 
This will create ''newdir'' and ''newdir/newdir2'', even if the former doesn't exist.
 
This will create ''newdir'' and ''newdir/newdir2'', even if the former doesn't exist.
   
  +
== grep ==
* ''xxdiff''
 
  +
grep 'pattern' filename
  +
This will search the file ''filename'' for the pattern ''pattern'' and displays the line on which it occurs
  +
grep 'pattern' *
  +
This searched for ''pattern'' in all files in the current directory. Very useful for looking through lots of code!
  +
 
== xxdiff ==
 
xxdiff file1 file2
 
xxdiff file1 file2
 
Much clearer than regular ''diff'' (graphical output).
 
Much clearer than regular ''diff'' (graphical output).
   
* ''svn''
+
== svn ==
 
The subversion version control program. Detailed info is available on the [[SVN Page]]
 
The subversion version control program. Detailed info is available on the [[SVN Page]]
   
* ''find''
+
== find ==
 
Can do so much, in a somewhat idiosyncratic manner! See this [http://www.hccfl.edu/pollock/Unix/FindCmd.htm guide].
 
Can do so much, in a somewhat idiosyncratic manner! See this [http://www.hccfl.edu/pollock/Unix/FindCmd.htm guide].
  +
  +
== rename ==
  +
Renames multiple files according to the patterns provided. A little care must be taken as there are two commonly used versions that have wildly different syntax. The sector machines all use the C version, some others (including arwen) use the Perl version. To check, run ''man rename''. If the first line contains LINUX PROGRAMMER'S MANUAL, then use the C syntax, otherwise use the Perl syntax.
  +
  +
To rename all files foo1.txt foo2.txt ... foon.txt to bar1.txt bar2.txt ... barn.txt:
  +
  +
rename foo bar foo* (C)
  +
rename 's/foo/bar/' foo* (Perl)
  +
  +
The Perl version uses a Perl expression: typically search and replace. For more details see the excellent guide in ''man perlrequick'' and the references therein.

Latest revision as of 07:04, 1 May 2008

Here are a few simple linux commands that should make life a bit easier for you. Please everyone, add anything you think would be useful!

You may want to read the computer office's introduction to Unix booklet .

Also, here is a really useful guide to bash scripting.

NOTE: You can always look up a command using man

man ls

This will display info on the options associated with the ls command. To exit the info page, press q.

ls

ls

This lists the contents of the current directory

ls -lrth

This will list the contents of the current directory showing more information and sorting them so the most recently modified file will be displayed last. The h flag makes the file sizes more human friendly!

ls -a

This will list all files i.e. the normal ls command will not display files beginning with a . i.e. .bashrc

cd

The 'change directory' command

cd
cd ~ 
cd /home/username

These all take you to your home directory

cd ../..

This takes you up two levels of the directory tree

cd newdir

This takes you into the directory called newdir

cd ~/Documents

This takes you into you Documents folder (actually located at /home/username/Documents)

cd ~csw34

This takes you into the home directory of user csw34

cd /sharedscratch/username

This takes you to your sharedscratch directory (only on clusters)

cp

cp file1 file2 newdir/

Copies file1 and file2 to the directory newdir

cp -r newdir /home/csw34

Recursively copies the directory newdir and all its subdirectories to csw34's home directory.

WARNING: you can overwrite files using cp i.e. if in the first example file2 already exists, you will replace its contents with those of file1!

For information on copying files between machines see the guide for scp linked from the Wales Group page.

mv

mv oldfilename newfilename

This effectively renames the file oldfilename to newfilename

mv test ../

This moves the file test one level up the directory tree.

WARNING: you can overwrite files using mv just like when you use cp so be careful!

mkdir

mkdir newdir newdir2

This will create two new directories, newdir and newdir2

mkdir -p newdir/newdir2

This will create newdir and newdir/newdir2, even if the former doesn't exist.

grep

grep 'pattern' filename

This will search the file filename for the pattern pattern and displays the line on which it occurs

grep 'pattern' *

This searched for pattern in all files in the current directory. Very useful for looking through lots of code!

xxdiff

xxdiff file1 file2

Much clearer than regular diff (graphical output).

svn

The subversion version control program. Detailed info is available on the SVN Page

find

Can do so much, in a somewhat idiosyncratic manner! See this guide.

rename

Renames multiple files according to the patterns provided. A little care must be taken as there are two commonly used versions that have wildly different syntax. The sector machines all use the C version, some others (including arwen) use the Perl version. To check, run man rename. If the first line contains LINUX PROGRAMMER'S MANUAL, then use the C syntax, otherwise use the Perl syntax.

To rename all files foo1.txt foo2.txt ... foon.txt to bar1.txt bar2.txt ... barn.txt:

 rename foo bar foo*  (C)
 rename 's/foo/bar/' foo* (Perl)

The Perl version uses a Perl expression: typically search and replace. For more details see the excellent guide in man perlrequick and the references therein.