FurtherTopics/Linux Command Line Advanced

From HPC
Jump to: navigation , search

Back to Command Line Quickstart

Back to Further Topics

Basic Commands

Commands that allow you to move around and create files within your file system can be found here.

Alternatively if you were looking for File Permissions, these can be found here.

man

This will explain the use of man pages (also called manual pages) on Linux. Most Linux files and commands have pretty good man pages to explain their use. Type man followed by a command (for which you want help) and start reading. Press q to quit the man page. See below:


username@viper:~$ man whois  (shows manual page for the command whois)
username@viper:~$ man syslog.config  (shows the manual page for a configuration file)
username@viper:~$ man syslogd (show the manual for a daemon (background program))
username@viper:~$ man –k syslog  (an apropos which shows a list of available man pages with this string contained within it)

Additional File Commands

file

This command determines the file type. Unlike Windows, Linux does not determine the file type from the extension but from examining the file header/contents itself.


username@viper:~$ file mypicture.png
pic33.png: PNG image data, 3840 x 1200, 8-bit/color RGBA, non-interlaced

username@viper:~$ file parallel.c
parallel.c: ASCII C program text

head

By default, the head command will show the first ten lines of a file.


username@viper:~$ head /etc/passwd

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh

tail

Similar to head but this time it will show the last 10 lines of the file by default.


username@viper:~$ tail /etc/services
vboxd 20012/udp
binkp 24554/tcp 		# binkp fidonet protocol
asp 27374/tcp 		# Address Search Protocol
asp 27374/udp
csync2 30865/tcp 		# cluster synchronization tool
dircproxy 57000/tcp 	# Detachable IRC Proxy
tfido 60177/tcp 		# fidonet EMSI over telnet
fido 60179/tcp 		# fidonet EMSI over TCP

tac

Works the same as cat but will show you the file backwards:


username@viper:~$ cat numbers
one
two
three

username@viper:~$ tac numbers
three
two
one

more

The more command is useful for displaying files that take up more than one screen. More will allow you to see the contents of the file page by page. Use the space bar to see the next page, or q to quit. Some people prefer the less command to more.

less

Very similar to more but with some additional features

find

This command is very useful to find files, more options are provided on the command line by typing man find. Here are some useful examples below:


username@viper:~$ find /etc   (find all files in the /etc directory)
username@viper:~$ find . –name “*.conf”   (find all files that end in .conf from the current directory)
username@viper:~$ find . –newer file1.c   (find all files newer than file1.c)
username@viper:~$ find /etc >etcfiles.txt   (find all files but this time put them in (pipe) to the file etcfiles.txt)

locate

The locate command is very different from find in that it uses an index to locate files. This is a lot faster than traversing all the directories, but it also means that it may be outdated too.

grep

The grep filter is famous among Linux (and UNIX) users. The most common use of grep is to filter lines of text containing (or not containing) a certain string.


username@viper:~$ grep “Darren” /etc/passwd 
user:x:1000:1000:Darren Bird,,,:/home/user:/bin/bash

As with most Linux commands, there are also a large number of useful options that will go with each command and grep is certainly no exception here


username@viper:~$ grep –i “Darren” /etc/passwd  (search in a case insensitive way)
username@viper:~$ grep –r “Darren” /etc/passwd  (search recursively down any directories too)
username@viper:~$ grep –v “Darren” /etc/passwd  (search for everything not containing “Darren”)

wc

Counting words, lines, and characters are easy with wc.


username@viper:~$ wc myfile.c  (show number of words, lines, and characters)
5 10 100 tennis.txt

Compression Commands

gzip – gunzip

This is a useful compression program (like zip which also exists in Linux). The gzip command can make files take up less space.


username@viper:~$ gzip myfile.c   (will create myfile.c.gz)
username@viper:~$ gunzip myfile.c.gz   (will create myfile.c again)

bzip2 – bunzip2

Files can also be compressed with bzip2 which takes a little more time than gzip, but compresses better.


username@viper:~$ bzip2 myfile.c  (will create myfile.c.bz2)
username@viper:~$ bunzip2 myfile.c.bz2  (will create myfile.c again)

zip – unzip

A compression program which is compatible with other zip programs found in MS Windows and other OSes.


username@viper:~$ zip myfile.c   (will create myfile.c.zip)
username@viper:~$ unzip myfile.c.zip   (will create myfile.c again)

Other Commands

date

The date command can display the date, time, time zone, and more.


username@viper:~$ date
Sat Feb 23 12:44:30 BST 2017

cal

The cal command displays the current month, with the current day highlighted.


username@viper:~$ cal
     April 2016
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30


sleep

The sleep command is sometimes used in scripts to wait a number of seconds. This example shows a five-second sleep.


username@viper:~$ sleep 5  (five seconds later)
username@viper:~$

sort

The command sort will sort lines of text files. By default, the output is to the screen but this can be piped to a file or another program.


username@viper:~$ sort myfile.txt
apple
banana
cherry

time

The time command can display how long it takes to execute a command. The date command takes only a little time.


username@viper:~$ time date
Sat Feb 23 13:08:27 BST 2016
real 0m0.014s
user 0m0.008s
sys 0m0.006s


Back to Further Topics / Back to Command Line Quickstart / Main Page