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:
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:
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.
user@unix:~$ sh deploy.sh | tee deploy.txt
To create symbolic links we can use the ‘ln’ command. For instance:
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:
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:
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:
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:
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:
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.
user@unix:~$ ls | wc -l