LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 08-22-2009, 02:54 AM   #1
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Post and share your one liner


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
Just to start

Last edited by micxz; 08-22-2009 at 01:54 PM.
 
Old 08-22-2009, 10:02 AM   #2
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by micxz View Post
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
grep file | grep -v ^# | grep .

# Get memory total (lol)
a1=`ls -la /proc/kcore | awk '{print $5}' | tr -d ''`;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
In the second example you should use cat instead of the first grep.

In the third example you can omit pipe tr -d ''.

In the fourth example you should use /var/log/httpd/ directory.
 
1 members found this post helpful.
Old 08-22-2009, 12:31 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by micxz View Post
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'
It's more of an old "tips 'n tricks" thread but I'm sure there's oneliners too: http://www.linuxquestions.org/questi...d-hints-27957/


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"

Code:
syscallName2Num() { grep "^#define __NR_${1}[[:blank:]]" $HDRLOC | awk '{print $3}'; }
syscallNum2Name: do the reverse
Code:
syscallNum2Name() { grep "^#define __NR_.*[[:blank:]]$1$" $HDRLOC | awk -F'_' '{print $4}'; }
shapertop: for watching the Wondershaper do it's work (or not)
Code:
shapertop() { /sbin/iptables -n -t mangle -L SHAPER -v -x | sort -grk1 | column -t; }
a variant of your "remove comments and blank lines":
Code:
stripcomments() { grep -v ^# | grep -v ^$; }
and a convoluted way to cut say lines 21 to 50 from some plaintext file:
Code:
showlines() { arg=($@); lines=$(wc -l ${arg[0]} | awk {print $1}); arg[1]=$[${lines}-${arg[1]}]; arg[2]=$[${lines}-${arg[2]}]; echo $-${arg[1]},$-${arg[2]} p | ed -s ${arg[0]}; }
 
Old 08-22-2009, 01:59 PM   #4
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Original Poster
Rep: Reputation: 75
Quote:
Originally Posted by w1k0 View Post
In the second example you should use cat instead of the first grep.

In the third example you can omit pipe tr -d ''.

In the fourth example you should use /var/log/httpd/ directory.
Yer right' I fixed the first two but I'm hoping people will know where they're error_log is.

&Thanks_for_that(unSpawn);
 
Old 08-22-2009, 03:03 PM   #5
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Rep: Reputation: 234Reputation: 234Reputation: 234
The first command determines current date and ``highlights'' it in the output of cal command using < and >:

Code:
set date `date`; /usr/bin/cal | sed -e 's/^/ /' -e "s/ $4$/<$4>/" -e "s/ $4 /<$4>/"
Code:
      August 2009    
 Su Mo Tu We Th Fr Sa
                    1
  2  3  4  5  6  7  8
  9 10 11 12 13 14 15
 16 17 18 19 20 21<22>
 23 24 25 26 27 28 29
 30 31
The second command allows to perform different calculation in command line interface or in scripts:

Code:
alias c='perl -e '\''$_="@ARGV"; print eval $_, "\n"'\'''
I described it in that thread: Using Perl to perform calculations in Bash.
 
1 members found this post helpful.
Old 08-22-2009, 03:22 PM   #6
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Rep: Reputation: 234Reputation: 234Reputation: 234
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`

Last edited by w1k0; 08-22-2009 at 03:40 PM.
 
Old 08-22-2009, 09:57 PM   #7
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Original Poster
Rep: Reputation: 75
w1k0 I added those to my mini script folder. Thanks'

Here's another cool trick I read in Linux Server Hacks:

Filename "ssh-to"
Code:
#!/bin/sh

ssh `basename $0` $*;
Then within PATH make symlinks to that script with your favorite hostname. My bin folder looks like:
Code:
-rwxr-xr-x 1 micxz users   ssh-to
lrwxrwxrwx 1 micxz users   mars -> ssh-to
lrwxrwxrwx 1 micxz users   pluto -> ssh-to
Then you can simply type "hostname" and bang you have a shell on that server. (tip: even cooler if you've setup keys)
 
1 members found this post helpful.
Old 08-24-2009, 01:04 AM   #8
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Original Poster
Rep: Reputation: 75
I just learned you can browse remote file systems via ssh in konqueror with the url fish://user@host
 
Old 10-16-2010, 06:02 PM   #9
pkramerruiz
LQ Newbie
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 19

Rep: Reputation: Disabled
OpenOffice.org splash screen (Ubuntu-Gnome)

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
 
Old 10-16-2010, 06:35 PM   #10
lumak
Member
 
Registered: Aug 2008
Location: Phoenix
Distribution: Arch
Posts: 799
Blog Entries: 32

Rep: Reputation: 111Reputation: 111
# download to stdout and extract with tar
curl <url> | tar czf -
 
Old 11-15-2010, 07:23 AM   #11
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Rep: Reputation: 234Reputation: 234Reputation: 234
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.
 
Old 11-15-2010, 08:29 AM   #12
dv502
Member
 
Registered: Sep 2006
Location: USA - NYC
Distribution: Whatever icon you see!
Posts: 642

Rep: Reputation: 57
A quick way to check which device node your USB stick is using. Plug in your USB stick and enter code in terminal.

Code:
dmesg | egrep hd.\|sd. | grep 'logical'
Example output from my terminal

sd 0:0:0:0: [sda] 180941040 512-byte logical blocks: (92.6 GB/86.2 GiB)
sd 0:0:1:0: [sdb] 29886400 512-byte logical blocks: (15.3 GB/14.2 GiB)
sd 4:0:0:0: [sdc] 7837696 512-byte logical blocks: (4.01 GB/3.73 GiB)

As you can see, sdc is the USB stick because it's the capacity of the USB stick eg. 4 GB.

Of course there are other ways to determine which device node your USB is using.

- Cheers

Last edited by dv502; 11-15-2010 at 08:32 AM.
 
2 members found this post helpful.
Old 11-15-2010, 09:31 AM   #13
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Rep: Reputation: 234Reputation: 234Reputation: 234
dv502,

Your one liner informs about device but omits an information about partitions. So I improved it slightly:

Code:
dmesg | egrep 'hd.|sd.' | grep 'logical' | grep `sed 's/.*\[//;s/\].*//' | tail -n 1` /var/log/messages | tail -n 5
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk one liner help niknak Linux - Newbie 1 05-07-2009 04:52 AM
IP Forward one-liner? JoeBleaux Linux - Networking 1 04-27-2009 10:24 AM
Help with Simple sed one liner SHARPY Linux - Newbie 10 04-12-2009 12:49 PM
Need one liner copy command madhi Linux - Software 2 07-31-2008 01:32 AM
Share your greatest one-liner!! PenguinPwrdBox General 3 08-20-2005 10:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration