Linux tips

Purpose of this document

Main goal of this page is to give some tips about the linux operating system using the BASH (Bourne-Again SHell).

program version
GNU bash 3.00.15
find 4.1.20
wc 5.2.1 (coreutils)
head 5.2.1 (coreutils)
tail 5.2.1 (coreutils)
cut 5.2.1 (coreutils)
xargs 4.1.20
rename 5.10.0 (perl)
lsof 4.78
netstat 1.42 (2001-04-15) (net-tools 1.60)

Search for files

To print the name of (shared) libraries from the directory $path that contains the defined symbol ($symbol) enter the following command:

[~] > find $path -name *.so -exec nm -A --defined-only {} \; | grep $symbol

To change recursively permissions of the directory $path and its subdirectories type the following shell command:

[~] > find $path -type d -exec chmod a+x {} \; -print

Count the number of lines, words and bytes

To count the number of static library file names that contains a given word ($word) enter the following command:

[~] > ls *.a | grep $word | wc -l

Select a line / column

To select the line ($line) from the file ($file) enter the following command:

[~] > head -n $line $file | tail -n 1

The first line of a file has the number 1.

To select the version number of the program swig that returns the version to stderr enter the following command:

[~] > swig -version 2>&1 | head -n 2 | tail -n 1 | cut -d' ' -f3

Special options of remove

To remove a file -filename that begins with the character - many forms are useful with the rm command. Use the option -- that indicates there is no more option after:

[~] > rm -- -filename

Or use the antislash character:

[~] > rm \-filename

Or give the absolute or the relative path of the file and write the file name using " characters around the file name:

[~] > rm .\"-filename"

Execute a command line from standard input

The xargs command is really powerful. As an example to remove some files from a directory $path filter with a regular expression $regex use pipes as follows:

[~] > ls $path/*.* | grep -E --regexp $regex | xargs rm -f

A way to change the permissions of all and only directories under a given directory $path:

[~] > find $path -type d | xargs chmod o+rx

Rename multiple files

To rename a list of files use the command rename. As an example to translate uppercase names to lower:

[~] > rename 'y/A-Z/a-z/' *

Is a directory empty?

To test if a directory is empty count the number of files with the ls command using options -A or --almost-all (do not list implied . and ..) and -1 (list one file per line):

> ls -A1 | wc -l .
0

Which Red Hat version?

Which Red Had version is installed:

> more /etc/redhat-release
Red Hat Enterprise Linux Server release 5.2 (Tikanga)

Which Debian version?

Which Debian version is installed (Ubuntu 9.04 Jaunty Jackalope):

> more /etc/debian-version
5.0

Information about opened ports

The file /etc/services is a plain ASCII file that maps textual names for internet services and their underlying assigned port numbers and protocol types. Every networking program should look into this file to get the port number and protocol for its service.

The program lsof list information about files opened by processes. Type the following command to see opened IPv6 listing port(s):

> lsof -Pnl +M -i6
COMMAND     PID     USER   FD   TYPE  DEVICE SIZE NODE NAME
eclipse 9147     1000   38u  IPv6 1824643       TCP 127.0.0.1:52855->127.0.0.1:40685 (ESTABLISHED)
eclipse 9147     1000   70u  IPv6 1824615       TCP *:33992 (LISTEN)
eclipse 9147     1000   74u  IPv6 1824647       TCP 127.0.0.1:33992->127.0.0.1:47189 (ESTABLISHED)

The following command list the programs that deals with a given port number:

> lsof -Pnl -i:33992
COMMAND     PID     USER   FD   TYPE  DEVICE SIZE NODE NAME
eclipse    9147  1000   70u  IPv6 1824615       TCP *:33992 (LISTEN)
eclipse    9147  1000   74u  IPv6 1824647       TCP 127.0.0.1:33992->127.0.0.1:47189 (ESTABLISHED)
python2.6 31362  1000    5u  IPv4 1824646       TCP 127.0.0.1:47189->127.0.0.1:33992 (ESTABLISHED)

The program netstat list the network connections, routing tables, interface statistics, masquerade connections and multicast membership. To see the active internet connections enter:

> sudo netstat -tulpn
Proto Recv-Q Send-Q Adresse locale          Adresse distante        Etat       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      2602/sshd       
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      3932/cupsd      
tcp6       0      0 :::33992                :::*                    LISTEN      9147/eclipse    
tcp6       0      0 :::22                   :::*                    LISTEN      2602/sshd

Useful options of find

find [path] ... [expression]

The GNU find utility searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence until the outcome is known, at which point find moves on to the next file name.

The first argument that begins with '-', '(', ')', ',', or '!' is taken to be the beginning of the expression; any arguments before it are paths to search, and any arguments after it are the rest of the expression. If no paths are given, the current directory is used. If no expression is given, the expression -print is used.

option description
-maxdepth <levels> Descend at most levels (a non-negative integer) levels of directories below the command line arguments.
'-maxdepth 0' means only apply the tests and actions to the command line arguments.
test description
-iname <pattern> Like -name, but the match is case insensitive.
-name <pattern> Base of file name (the path with the leading directories removed) matches shell pattern pattern. The metacharacters ('*', '?', and '[]') do not match a '.' at the start of the base name. To ignore a directory and the files under it, use -prune.
action description
-exec <command> Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of ';' is encountered. The string '{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with a '\') or quoted to protect them from expansion by the shell. The command is executed in the starting directory.
-print True; print the full file name on the standard output, followed by a newline.
-prune If -depth is not given, true; do not descend the current directory.
If -depth is given, false; no effect.

Useful links

link comment
BASH home page the BASH web site
BASH manual the BASH manual
Red Hat web site the official Red Hat web site