I decided to write a post which will be constantly updated with some linux hints or commands. Why ? Simply because there are a lot of commands that each time I need them, I don’t exactly remember how to use them without searching for examples on Internet. Don’t hesitate to write a comment if you have an interesting command that you often use 🙂
Sometime, it is necessary to search some specific files on the file system. Theses following commands are the most used in my case.
find ./ -name myfile.txt
find / -name myfile.txt
cd /; ls -lR | grep myfile.txt
Note: Keep in mind to execute the command updatedb for update the indexing file database which is used by tools like locate
After some tests, I use often locate command because if the file is indexed, the searching is seriously faster 🙂
I am often confronted to read some log files but theses files can potentially contains a huge amount of text data. If you know what do you looking for, it could be useful to filter the output of a command. In this example, I will filter the output of a netstat command.
netstat -a | egrep "tcp"
netstat | egrep "tcp6|udp6"
In this second example, the pipe is used to make a OR condition. To make a AND condition with egrep, I just apply a second egrep, but it is possible in one command with grep.
netstat -a | egrep "tcp" | egrep "LISTEN"
netstat -a | grep "tcp.*LISTEN"
Theses commands are very simple but often sufficient to looking for specific things in a text output. I will not write example using REGEX pattern for the moment because it is a little hard to be in a cheatsheet I think.
Sniffing the traffic with TCPdump
Sometime, when I work on a remote server without graphical interface, it is not always possible to start graphical tools like Wireshark because the X server forwarding is not enable. My last chance is to use console tool like tcpdump. Please note that tcpdump must be run as root.
tcpdump -i eth0
tcpdump -vvv -i eth0
tcpdump -i eth0 port 80
It is possible to specify a port using some protocol name. The next command has the same effect that the previous one.
tcpdump -i eth0 port http
TAR commands can be sometime hard to memorize but I am often confronted against TAR archive.
tar cvf myArchive.tar ./dirToArchive/
To archive a directory, memorize the parameter “c” which could mean “Compress” even no compression is made with this command. But it is helpful for me.
tar xvf myArchive.tar
To extract, keep in mind the “x” which stands for “eXtract”.
tar tvf myArchive.tar
I have no hint to memorize the “t” option.
tar cvzf myArchive.tar.gz ./dirToArchive/
“z” for for “Zip” 🙂
tar xvzf myArchive.tar.gz
To be continued 🙂