Familiarizing with command-line tools

pwd – this command prints the name of current directory.

$ pwd
/home/vagrant

ls – command ls is used to view the contents of directory

$ ls
data-science  R  repos

cd – command cd is used to navigate to different directories

$ cd R/x86_64-pc-linux-gnu-library/
$ cd ..

head – command head is used to view the first few lines of file. Values which come after the command are called command-line arguments or options.

$ head -n 5 data-science/data-science-at-the-command-line-master/book/ch02/data/movies.txt
Matrix
Star Wars
Home Alone
Indiana Jones
Back to the Future

grep – it is used to filter lines.
wc – wc is used to count lines.
sort – it is used to sort lines.
seq – seq generates a sequence of lines.

backslash (\) is used to break up a long command to multiple lines.

$  echo 'ahilan'\
> 'kana'
ahilankana

(>) is the continuation prompt, which indicates that this line is a continuation of the previous one.

Shell function – A shell function is a function that is executed by bash.

$ fac() { (echo 1; seq $1) | paste -s -d\* | bc; }
$ fac 6
720

Alias – aliases are often defined in .bashrc or .bash_aliases configuration files. Currently defined alias can be found by running alias command.

Identifying the type of command line tool

$ type -a pwd
pwd is a shell builtin
pwd is /bin/pwd

$ type -a fac
fac is a function
fac ()
{
    ( echo 1;
    seq $1 ) | paste -s -d\* | bc
}

Combining Command-line tools – command-line tools are combined through pipe (|).

# seq generates teh sequence of numbers
$ seq 20

# we can pipe the ouput of seq to a second command-line tool (grep) to filter lines.
$ seq 20 | grep 4
4
14

#if we wanted to know how many numbers between 1 and 100 contain a “5”.
$ seq 100 | grep 5 | wc -l
19

Save the output of command-line tools

# if numbers.txt file is available, the following command will overwrite it
seq 100 > numbers.txt
# though numbers.txt file is available, the following command will append the following output with existing one.
seq 20 >> numbers.txt

mv – is used to rename or move entire directories.
rm – is used to remove the files. Option -r is used to delete the files recursively.
cp – is used to copy the files.
mkdir – is used to create directory.
man – is used to find the help of command-line tools.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s