Unix basic commands

This is a recompilation of helpful commands for Unix systems.

 

List directory content

To list the content of a specific directory we can use the ‘ls’ command. The syntax is ls [options] [directory] being the ‘-l’ a really helpful option, which shows the directory content with details:

1
2
3
user@unix:~$ ls -l
drwxr-xr-x 2 user user 4096 2010-03-10 00:03:45 Desktop
drwxr-xr-x 2 user user 4096 2010-01-19 22:33:05 Documents

 

The pipe “|” command

This command allows us to pipe the outputs and inputs of other processes. The concatenation of pipe commands is very helpful and powerful, and it is really used on Unix systems. An example is:

1
user@unix:~$ cat fichero1 fichero2 | grep palabra | sort | uniq

 

The tee command

tee is a command using standard streams which reads standard input and writes it to both standard output and one or more files, effectively duplicating its input. It is primarily used in conjunction with pipe.

1
user@unix:~$ sh deploy.sh | tee deploy.txt

 

Symlinks

To create symbolic links we can use the ‘ln’ command. For instance:

1
user@unix:~$ ln -s /path/to/link [new_alias]

With the above command we create a file named ‘new_alias’ which points out to the ‘/path/to/link’.

 

Show the last lines of a file

The command ‘tail’ shows the last lines of a file. For instance:

1
user@unix:~$ tail -f -n 10 file.txt

Shows the last 10 lines of the file.txt file.

 

Show the command documentation

A really helpful command is the ‘man [command]’ command which shows the documentation of the given command , for instance:

1
user@unix:~$ man tail

It will show the documentation for the tail command.

 

Flush DNS cache

If we need to flush our DNS cache we could run:

1
user@unix:~$ sudo /etc/init.d/dns-clean restart

 

Count files and/or folders

Sometimes we need to kwno how many files and/or folders we have in a specific path, to do that we could run:

1
user@unix:~$ ls -lR | grep ^d | wc -l

 

Check disk usage

If we want to check the usage of our hard disk we should run:

1
user@unix:~$ du -h -a /dir | grep "[0-9]G\b"

 

Count files in folder

Note that it doesn’t count hidden files and assumes that file names don’t contain newline characters.

1
user@unix:~$ ls | wc -l