Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Haven't found a thread here where people are sharing there favorite one liners (except "Share your greatest one-liner!!") which ended pretty fast in 05'
I know allot of one liners will have to be built as needed and most will not likely be used everyday. Some maybe useless and some useful. But I find it fun to share and I hope people will benefit from this and maybe even some new aliases will be born!
[Please use code tags]
Code:
# Find last occurance of pattern and append string:
num=`awk '/<pattern>/ { print NR }' filename | tail -1`; sed "$num a <string>" filename
# Remove comments and blank lines
cat file | grep -v ^# | grep .
# Get memory total (lol)
a1=`ls -la /proc/kcore | awk '{print $5}'`;echo $(($a1/1024/1024));
# grep top 20 404's
grep "File does not exist" error_log | awk '{print $13}' | sort | uniq -c | sort -rn | head -20
Haven't found a thread here where people are sharing there favorite one liners (except "Share your greatest one-liner!!") which ended pretty fast in 05'
Most of my oneliners are functions. Examples:
secp: copy directory contents and keep SELinux contexts:
Code:
secp() { cd "$1" && tar --preserve --selinux -vcf - . | ( cd "$2" && tar --selinux -xBf - ); }
syscallName2Num: convert system call name to number (for Auditd logs mostly)
* both depend on HDRLOC="$(readlink -f /lib/modules/$(uname -r)/build)/include/asm-i386/unistd.h"
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309
Rep:
These aren't one liners but short scripts. I use them to find SUID or SGID files and directories, huge old files, huge files, huge directories, and files owned by the users or groups non-existing in the system.
1.1.suid
Code:
#!/bin/sh
# 1.1.suid seeks the system for the files having set SUID bit.
> 1.1.suid.`hostname`
echo "Still SUID or SGID files at `hostname`:" >> 1.1.suid.`hostname`
find / -mount -type f \( -perm -4000 -o -perm -2000 \) -exec ls -ld '{}' \; | \
cut -d " " -f -4,8- >> 1.1.suid.`hostname`
1.2.suid
Code:
#!/bin/sh
# 1.2.suid seeks the system for the directories having set SUID bit.
> 1.2.suid.`hostname`
echo "Still SUID or SGID directories at `hostname`:" >> 1.2.suid.`hostname`
find / -mount -type d \( -perm -4000 -o -perm -2000 \) -exec ls -ld '{}' \; | \
cut -d " " -f -4,8- >> 1.2.suid.`hostname`
2.huge+old
Code:
#!/bin/sh
# 2.huge+old seeks the system for the huge and old files (the files over
# 1 MB, modified or used for the last time three months ago or before).
> 2.huge+old.`hostname`
echo "Huge and old files at `hostname`:" >> 2.huge+old.`hostname`
find / -mount -size +10000k \( -mtime +120 -a -atime +120 \) -exec ls -l {} \; | \
cut -d " " -f 5- | sort -nr >> 2.huge+old.`hostname`
3.huge-fil
Code:
#!/bin/sh
# 3.huge-fil seeks the system for the huge files exclusively.
> 3.huge-fil.`hostname`
echo "Huge files at `hostname`:" >> 3.huge-fil.`hostname`
find / -mount -size +10000k -exec ls -l {} \; | \
cut -d " " -f 5- | sort -k4 >> 3.huge-fil.`hostname`
4.huge-dir
Code:
#!/bin/sh
# 4.huge-dir seeks the system for the huge directories.
> 4.huge-dir.`hostname`
echo "Huge directories at `hostname`:" >> 4.huge-dir.`hostname`
du -B 1000 / | grep -E '^[0-9]{7,}' >> 4.huge-dir.`hostname`
5.nobody
Code:
#!/bin/sh
# 5.nobody seeks the system for the files without any association to the
# user or group existing in the system.
> 5.nobody.`hostname`
echo "Nouser or nogroup files at `hostname`:" >> 5.nobody.`hostname`
find / -mount -nouser -o -nogroup | egrep -v "/dev/" >> 5.nobody.`hostname`
To hide splash screen:
sudo sed -i 's/Logo=1/Logo=0/g' /etc/openoffice/sofficerc
To unhide the splash screen:
sudo sed -i 's/Logo=0/Logo=1/g' /etc/openoffice/sofficerc
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309
Rep:
This is one liner formatting text files with long lines. It breaks each line after 76 column and inserts empty lines between paragraphs. I called that script format:
Code:
#!/bin/sh
# formats text data
cat "$1" | perl -pe "s/^/.ll 76\n.na\n.nh\n/;s/\n/\n\n/" | nroff | cat -s
To format file.txt use the commands format file.txt in order to display the contents of the file or format file.txt > file-after-formatting.txt in order to store the contents in a new file.
I used tricky Perl command inserting nroff commands to the file before processing it with nroff. I’m sure there’s easier method of achieving the same result with AWK.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.