The usefull linux commands
Introduction
How to Find and Delete Log Files Older Than 30 Days
# find /path/to/log/ -type f -name \*.log\* -mtime +30 -exec rm {} \; > /dev/null 2>&1
How to Find and Delete Log Files Less Than 30 Days Old
Similarly, you might need to find and delete log files that are less than 30 days old. The following command will help you do just that:
# find /path/to/log/ -type f -name \*.log\* -mtime -30 -exec rm {} \; > /dev/null 2>&1
Create a Directory and Move into It at the Same Time
Creating directories and navigating into them is a common task. Here’s a one-liner that allows you to create a directory and move into it at the same time:
# mkdir test-dir && cd $_
Display the Size of a Folder
Knowing the size of directories is crucial for managing disk space. You can display the size of a folder with the following command:
# du -csbh /path/to/folder
Display Output as a Table
Displaying data in a tabular format can make it easier to read. Use the following command to format the /etc/passwd
file as a table:
# cat /etc/passwd | column -t -s:
Run the Last Command as Root
Sometimes, you may need to re-run the last command with root privileges. Instead of typing the entire command again, you can simply use:
# sudo !!
Return to the Previous Directory
If you need to quickly navigate back to the previous directory, this command will save you time:
# cd -
Add a Passphrase to Your id_rsa
File
For enhanced security, you can add a passphrase to your id_rsa
file using the ssh-keygen
command:
#ssh-keygen -f id_rsa -p
Combine wget
and tar
in One Command
Downloading and extracting files can be done in one step. Here’s how you can combine wget
and tar
in a single command:
# wget -qO- http://link-download | tar xvz
Combine find
and xargs
Commands
To perform actions on files found by the find
command, you can combine it with xargs
:
# find /home/huupv -name *.jpg | xargs -d '\n' ls -ll
Match All IP Addresses in a Log File with grep
To find all IP addresses in a log file, use the following grep
command with a regular expression:
grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"
Check for Duplicate Files
To identify duplicate files in a directory, use this combination of find
and md5sum
:
find /home/huupv/ -type f -print0 | xargs -0 md5sum | sort | uniq -D -w 32
Determine Your Public IP Address
To quickly find your public IP address, use one of these commands:
curl ipinfo.io/ipor
curl -s checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//'
Comments
Post a Comment