In this post I will list the most useful commands I’ve come across so far.
Note about options
Commands and other programs can have options that are very interesting to use in some cases. An option is like the ‘way’ the command will execute what you asked from it. They are like flags you put after the command / program you want to run. They usually start by ‘-‘ or ‘–‘.
For example:
‘-v’ is an option that can mean ‘verbose’.
‘-h’ usually stands for ‘help’
The commands I will present have options but I will not go through their options.
A doubt about how to use a command? What options can be used?
The commands man
(manual) and info
(information) have it all documented neatly and their pages are available through the terminal as follows
man 'command'
info 'command'
The manual has a manual page:
man man
You can get information about info:
info info
Navigating in folders (and files)
cd /path/to/directory
# Change Directory
ls
# List directory content
pwd
# Prints the complete path of the current/working directory
Create, move, remove folders and/or file
mv file1.txt file2.txt
# rename file1.txt into file2.txt (will overwrite file2.txt if it exist, check -i option)
mv file1.txt new/directory
# move file1.txt to a new directory
cp file1.txt file2.txt
# makes a copy of file1.txt called file2.txt
cp file1.txt new/directory
# Copy file1.txt to a new directory
mkdir directory_name
# Make directory
rmdir directory/to/remove
# remove directory (only empty ones)
rm file.txt
# Remove the file.txt (CAREFUL with this one. There is no coming back)
Getting information on files
head input.txt
# Get the 10 first line of the input.txt
tail file.txt
# Get the 10 last line of the input.txt
grep 'pattern' input.txt
# Outputs all the lines of input.txt which contain ‘pattern’
Some options for grep are very useful:
-o will output only the part of the line of the input file corresponding to the pattern
-v will invert the match (output all the lines which do not have the pattern)
-c will count the number of lines in which the pattern appears
Regular Expressions can be used in the pattern. See the wikipedia page for further information; http://en.wikipedia.org/wiki/Regular_expression. Also I hope to post about them soon.
diff file1.txt file2.txt
# Outputs the differences between file1.txt and file2.txt
wc file.txt
# Prints on the terminal the number of newlines, words, and byte counts for file.txt (can be used with more then one file)
Manipulating files
cat file1.txt file2.txt
# Concatenate file1.txt and file2.txt. The output will be the lines of file1.txt followed by the lines of file2.txt
cut
# remove sections from each line of files
cut -d ',' -f 2,7 file.txt
# Keep columns 2 and 7 from file.txt which has its columns delimited by commas (-f option stands for field; -d option stands for delimiter of the fields). This is usually used when you want to keep only certain columns of a table
paste
# Merge lines of files.
paste file1.txt file2.txt
# The first line of file2.txt will be put next to (on the same line as) the first line of file1.txt in the output. This is usually used if you want to create a table where some of the columns are on one file and others on another. Just remember that the lines must correspond to each other (line1 with line 1 of the second file, line 2 with line 2, etc)
join file1.txt file2.txt
# join lines of two files on a common field
sed 's/thing-to-replace/replacement-text/' file.txt
# Stream Editor, the ‘s’ means you are doing a substitution of the ‘thing to replace’ by the ‘replacement-text’. You can also use Regular Expressions in the text to replace.
sed '1,2 d' file.txt
# Delete lines 1 and 2 from file.txt
tr
# translate or delete characters.
tr ',' '\t' < file.txt
# Replace the commas by tabs in file1.txt
WARNING FOR MAC USERS: ‘\t’ which stands for ‘tab’ in Ubuntu does not work for Mac OS. To enter the ‘tab’, press [Control] + [V], followed by [Tab]
Text Editors
You can open a text editor by just typing its name on the terminal.
nano
or pico
# Small Text Editors that open in the terminal. When you open them, you will see the keys you must use to save, exit, etc. The ‘^’ symbol means ‘control’.
emacs
vim
gedit
# for ubuntu users
Others
bc
# Basic calculator. In ‘bc’ you will be able to do some calculations.
history
# lists the recent command lines you’ve used
Ok, that’s it for now. If you know these commands and how to use them you can do the GREAT deal of things.