Command Line Program Examples

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Joined: May 22, 2004
  • Posts: 3415
  • Loc: Richland, WA
  • Status: Offline

Post June 9th, 2009, 1:20 pm

I was thinking it might be a good idea to have a resource in the Unix/Linux forum where people can post how to use command line programs. More so interesting ways to use them, or just things you can do with them that the beginning Linux user might not know and maybe looking for.

For example:

You can run the following command to see memory usage of currently running process for a specific user.

Code: [ Select ]
ps -u root -o rss,command


You can add the following to remove certain commands with the given phrase in the command.

Code: [ Select ]
grep -v string


You can also add the following to sum the first column and return the results, in this case its the memory used and it returns the value in MB.

Code: [ Select ]
awk '{sum+=$1} END {print sum/1024}'


All elements together:
Code: [ Select ]
ps -u root -o rss,command | grep -v string | awk '{sum+=$1} END {print sum/1024}'


Do you think this would be a good resource to add to the forum?

I wouldn't expect this to be a sub category, more of just an organized thread. I just wanted to get some feedback on the idea.
#define NULL (::rand() % 2)
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post June 9th, 2009, 1:20 pm

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Joined: Dec 20, 2002
  • Posts: 8925
  • Loc: Seattle, WA & Phoenix, AZ
  • Status: Offline

Post June 9th, 2009, 2:40 pm

I think its a good idea and something that could be useful. I use the command line all the time to run and administer linux servers for websites which include ozzu. Some of the most important things I do involve using the command line and pico (not into vi).
Ozzu Hosting - Want your website on a fast server like Ozzu?
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post June 9th, 2009, 4:04 pm

NOT into vi?! :shock: j/k. :) I do like the idea. Perhaps I can throw in a few pointers here and there as well. All of our VM servers @ work are linux, so I'm in the same boat.
I'd love to change the world, but they won't give me the source code.
  • kc0tma
  • o|||||||o
  • Web Master
  • User avatar
  • Joined: Jul 20, 2007
  • Posts: 3318
  • Loc: Trout Creek, MT
  • Status: Offline

Post June 10th, 2009, 6:12 am

Code: [ Select ]
who | sort | uniq | wc -l


...will tell you how many users are logged onto a system. I put that into a little shell script then put that script in /usr/local/bin so I can call it up faster than you can snap your fingers.
Like Mr Spork, I also write about my interest in alcoholic beverages.
  • jitendraag
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Oct 27, 2008
  • Posts: 8
  • Status: Offline

Post June 10th, 2009, 7:51 am

The most used for me would be tar -xvzf filename.tar ;-)
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post June 10th, 2009, 1:29 pm

whoami is nice for determining the user a script interpreter or similar application able to pass commands to a shell is logged in as.

whereis is nice for determining where an application is.
Code: [ Select ]
joebert@home:~$ whereis php
php: /usr/bin/php /usr/share/man/man1/php.1.gz
  1. joebert@home:~$ whereis php
  2. php: /usr/bin/php /usr/share/man/man1/php.1.gz
Strong with this one, the sudo is.
  • middayc
  • Novice
  • Novice
  • User avatar
  • Joined: Jun 29, 2009
  • Posts: 23
  • Loc: Slovenia
  • Status: Offline

Post June 30th, 2009, 5:22 pm

nice topic (I didn't know for that usage of ps, and will need it)..
I collect linux commandline tricks that I need and find here and there on some wiki page because otherwise I would always forget them when I needed them. Here are few more interesting ones:

Count files with <error> in them:
Code: [ Select ]
grep -L "<error>" * | wc -l


Remove files that don't have this string in them
Code: [ Select ]
grep -L --directories=skip "<error>" * | xargs rm


Detach script from your shell session (& does that) so you can run other apps and so that it keeps executing after you close the ssh console (nohup does that):
Quote:
nohup wget http://www.co.com/some-big-file.zip &
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6130
  • Loc: Seattle, WA
  • Status: Offline

Post July 1st, 2009, 2:38 pm

Code: [ Select ]
fortune
The Beer Monocle. Classy.
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post July 1st, 2009, 3:20 pm

A majority of these are probably linux specific. That's all I really use, so it's what I know. ;) I didn't list all the flags associated with each command, but I tried to include the useful ones, which brings us to the first in my list:

man - the built-in help manual. Specify a command or app that you want info on. Format: man grep

ls - List contents of current directory (useful flags: -l, -a). Format: ls -a

df - Show used disk space and mounted drives (default returns size in blocks, use -h flag to specify common units). Format: df -h

locate - search utility. Keeps a db listing of files - generally faster returns than find. Format: locate [filename]

find - The locate db may not always be current enough to find what you need. In that case, use find. Format: find [path] -name [filename]

cat - displays the entire contents of a file. Not very useful for large files. Format: cat [filename]

head - view the top portion of a file. (-n flag sets the number of lines to view - default is 10 lines). Format: head -60 [filename]

tail - reverse of head - view the end of a file. (-n flag is the same, -f will follow a file live as it changes). Format: tail -n 60 -f

sudo - used when superuser rights are required for a command. Format: sudo tail -n 60 [filename]

ifconfig - Display the IP's that the system is using and the amount of traffic sent/received (-a flag to view all). Format: ifconfig eth0

top - Graphical real time process monitor. Format: top

ps - Overview of all running processes. Format: ps

kill - Kill a process (use top or ps to identify the PID and then plug that in). Format: kill -9 or killall -9

cp - Copy a file. Format: cp [orig filename] [destination path]

mv - Move a file. Format: mv [orig filename] [destination path]

scp - securely copy file over ssh. Format: scp [filename] [user]@[remote host]:/[path to file]

shutdown - Shut down the system (-r flag to set delay to 'now' or a number of minutes). Format: shutdown -r now

I've excluded grep from my list since it's been mentioned a few times, but I highly recommend that anyone using command line take a hard look at grep and play around with it until you're familiar with its basic capabilities. Entire books can (and have!) been written about grep. It's a powerful tool!
I'd love to change the world, but they won't give me the source code.
  • kapalpecah
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Jun 25, 2009
  • Posts: 13
  • Status: Offline

Post July 9th, 2009, 9:15 am

change string ocurance in all files

Code: [ Select ]
find . | xargs perl -p -i.old -e 's/needreplace/newstring/g'
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post July 24th, 2009, 1:20 am

Count the lines in a directory full of files, listing per-file and total results.

Code: [ Select ]
me@here:/lists$ ls
2009-07-22  2009-07-23  2009-07-24
 
me@here:/lists$ wc -l *
  109 2009-07-22
  739 2009-07-23
   12 2009-07-24
  860 total
  1. me@here:/lists$ ls
  2. 2009-07-22  2009-07-23  2009-07-24
  3.  
  4. me@here:/lists$ wc -l *
  5.   109 2009-07-22
  6.   739 2009-07-23
  7.    12 2009-07-24
  8.   860 total


Count the unique lines in a file

Code: [ Select ]
me@here:/lists$ cat list.txt | uniq | wc -l
1490
  1. me@here:/lists$ cat list.txt | uniq | wc -l
  2. 1490
Strong with this one, the sudo is.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post August 8th, 2009, 4:12 pm

If you use RSA keys to login via SSH instead of being prompted for your password, you can use tab-completion while typing a remote file path for SCP.

Code: [ Select ]
scp me@remote:/complete-using-tab ...
Strong with this one, the sudo is.
  • hostechsupport
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Sep 24, 2006
  • Posts: 13
  • Status: Offline

Post January 16th, 2010, 8:30 am

If you want to delete files older than certain number of days then you can use the following command :-


BASH Code: [ Select ]
find /path/to/files* -mtime +10 -exec rm {} \;


where -mtime is used to specify the number of days old that the file is. If you enter +10 as shown in the command , it will find files older than 10 days.

And -exec, allows you to pass in a command such as rm(remove). The {} \; at the end is required to end the command.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post January 17th, 2010, 4:39 am

The -atime flag can be substituted for the -mtime flag there to delete files that haven't been accessed in N days. Works great for cache folders.
Strong with this one, the sudo is.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Joined: Dec 20, 2002
  • Posts: 8925
  • Loc: Seattle, WA & Phoenix, AZ
  • Status: Offline

Post January 30th, 2010, 5:13 pm

joebert wrote:
whereis is nice for determining where an application is.
Code: [ Select ]
joebert@home:~$ whereis php
php: /usr/bin/php /usr/share/man/man1/php.1.gz
  1. joebert@home:~$ whereis php
  2. php: /usr/bin/php /usr/share/man/man1/php.1.gz


Interesting never used that one before. I always use which:

Code: [ Select ]
[0.26] root@server1 [~]# which php
/usr/local/bin/php
  1. [0.26] root@server1 [~]# which php
  2. /usr/local/bin/php
Ozzu Hosting - Want your website on a fast server like Ozzu?
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post January 30th, 2010, 5:13 pm

Post Information

  • Total Posts in this topic: 25 posts
  • Users browsing this forum: No registered users and 108 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.