Here are some commands to help me with my work.
And what are your useful commands that you use?
Searching for files and folders with the given name
grep -i -n -r 'Search string' /var/www/path/
Shows how many times is the search text in the text and returns the result in numbers.
cat access.log|grep 'Search string' | wc -l
Remove all .gz from /var/log/
find /var/log/ -name "*.gz" -type f -delete
Find and replace text in all files in a directory
find ./ -type f -exec sed -i 's/string1/string2/g' {} \;
find /var/log/ -type f -exec sed -i 's/string1/string2/g' {} \;
Unpack tar files
tar -xJfv file.tar.xz
tar -xvf file.tar.bz2
while
while true; do COMMEND; done
Live viewing of web server logs
tail -f /var/log/nginx/korotkiewicz-access.log
Top comments (15)
Great commands! I use them a lot too!
Here are 15 networking commands that might help you a lot as well:
Top 15 Linux Networking tools that you should know
A few improvements to those commands I've found recently over the last few years...
grep: ripgrep or silver searcher, which know to ignore source control files by default and are just faster
bat: a syntax highlighting cat
also awk is surprisingly useful
One that comes in handy frequently for me is
zgrep
. It's the same as grep, but for files that are gzipped. It's helpful when logrotate is in use.I use
cut
to split and parse content a lot. The syntax is short, sweet and easy to remember:This prints the second field (
-f2
) delimited by a comma (-d,
). Useful for parsing CSV files.awk
is wonderfully powerful, too. Use it for simple result printing and formatting:But it does so much more. Since we're talking about searching text with
find
,sed
andgrep
,awk
lets you search for text based on its specific position in a file:Test can be combined:
This shows all running processes not associated with your own PID that match the shell variable
$VAR
.$$
is a special shell construct containing the session's process ID.ps -ef
) with PID and Parent PID in columns 2 and 3 respectively;|
) output toawk
;$VAR
and$$
toawk
variablesprogram
andmysid
;&&
; the "or" operator is||
): ** Check columns 2 and 3 do not matchmysid
(!= mysid
); ** Check the whole line ($0
) for a match withprogram
;'{print $0}'
) when all three conditions are met. Use this to test whether another process is already running a script (without using a lock file). Usebasename
to get the script name, look for other processes running it andexit
. As a bonus, exclude processes editing the file and not running it.is the same as (cat is not needed here):
Also I prefer not using
-v
in tar. It shows a lot of noise and I may miss some important error.Thanks for sharing your commands. 😀
In fact you can even remove the wc command with the -c flag of grep 😉
You're right! I'm always forgetting it can count.
My fingers will do
ls -altr
in my sleep.Mine is
ps -ef
My fingers
ls -lashx
.to get the last 20 lines of the error log is perhaps the most used command on my machine.
You're right, I forgot
tail
! I usetail -f file.log
to live track of my logs.Ideal for live viewing of web server logs.
history | grep "partial"
to search your command history for some long command you've used before but don't remember exactly.partial
is just whatever part you can rememberYou can also press CTRL + R and start writing the first letters of the command, and again press CTRL + R to find the next matches, next, and next...
I have a little terminal commands cheat sheet for myself, maybe it will be useful to someone else.