Quick and cool Linux commands!

  • Redirection in Linux
  • 0 = stdin
    1 = stdout
    2 = stderr

  • The following command saves stdout and stderr to the files “out.txt” and “err.txt”, respectively.
[root@server /root]# ./cmd 1>out.txt 2>err.txt
  • Using the tee command to save stdout to tee.txt. stdout is still displayed on the screen.
[root@server /root]# cmd | tee tee.txt
  • Using the tee command to append stdout to tee.txt. stdout is still displayed on the screen.
  • [root@server /root]# cmd | tee -a tee.txt
  • This is a cool ‘find’ command – looks in each file for ‘here’. First we create 3 files with the string ‘here’ in 2 of the 3 files
f@foxhound1:~/temp/findtest$ echo "searchstring is here">text1
f@foxhound1:~/temp/findtest$ echo "searchstring is also here">text2
f@foxhound1:~/temp/findtest$ echo "search string">text3
f@foxhound1:~/temp/findtest$ echo "string">text3

Then we issue the command

 find . -type f -exec grep -i here  \{\} --with-filename \;

And the corresponding output is:

f@foxhound1:~/temp/findtest$ find . -type f -exec grep -i here  \{\} --with-filename \;
./text1:searchstring is here
./text2:searchstring is also here

From the output, we can see that the command was able to search all three files we created, but only text1 and text2 contained the string ‘here’. To check that it does search all three files, we issue the command (with the corresponding output)

f@foxhound1:~/temp/findtest$ find . -type f -exec grep -i here  \{\} --with-filename \;
./text3:string
./text1:searchstring is here
./text2:searchstring is also here

Which proves that it does search all files (in this case, all 3 files we just created) since all 3 files have ‘string’ in them, as shown in the ouput.

The difference with using ‘find’ this way as compared to ‘xargs’ (see comment below) is that you can execute any command after the ‘exec’ parameter of ‘find’. You can delete (rm), view (cat) etc files found by ‘find’ this way.

Another neat and quick trick (especially when doing coding/software development) is to find a specific string (see above) within a directory, but to not include certain files containing the string you’re looking for (as with the case of using a version control system such as subversion):

find /var/www/@dmin/ -type f -exec grep up.edu.ph   \{\} --with-filename \;|sed -e '/svn/d'|sed -e '/~/d'

This chain of command (coupled with the above ‘find’ command) removes from the list of files ‘find’ found all the files that have ‘svn’ in their filenames, including the files that have a ‘~’ (tilde), which in Linux is equivalent to a backup/temporary file.
More quick and cool Linux commands can be found here.

Scripting

The following logs the date, the machine’s motherboard and CPU temperature (assumes you have lm-sensors properly setup) and the voltage level of a certain rail in the power supply, and puts all of those in a file called datemp.log:

date +%m/%d/%y%X|tr -d 'n' >>datemp.log&& sensors|grep +5V|cut -d "(" -f1|tr -d 'n'>> datemp.log && sensors |grep Temp |cut -d "(" -f1|tr -d 'n'>>datemp.log

Which after 5 times of iteration (say in a loop for logging, taken every second) gives the following nice output on datemp.log:

02/10/0805:53:48  PHT+5V:         +4.95 V  M/B Temp:    +52.0°C  CPU Temp:    +51.0°C  Temp3:       +17.0°C
02/10/0805:54:08  PHT+5V:         +4.92 V  M/B Temp:    +52.0°C  CPU Temp:    +51.0°C  Temp3:        +3.0°C
02/10/0805:54:29  PHT+5V:         +4.92 V  M/B Temp:    +52.0°C  CPU Temp:    +51.0°C  Temp3:       +13.0°C
02/10/0805:54:50  PHT+5V:         +4.92 V  M/B Temp:    +52.0°C  CPU Temp:    +51.0°C  Temp3:       +17.0°C
02/10/0805:55:11  PHT+5V:         +4.92 V  M/B Temp:    +52.0°C  CPU Temp:    +51.0°C  Temp3:       +30.0°C

This one-line logging technique is beneficial in two ways: the output log (in this case datemp.log) filesize is kept at a minimum, and makes it easier to look for desired values using ‘grep’ since values for every iteration is on a single line.

Advertisements

Tags: , , , , ,

4 Responses to “Quick and cool Linux commands!”

  1. Doing cool things in Bash and in Linux « F241vc15′ Weblog Says:

    […] want more info afterwards on other cool Linux and bash commands, you can consult my earlier post here. This post would probably mean it’s an extension of that previous post of mine. Difference is […]

  2. Cam McK Says:

    You can also use Xargs and grep to find strings. I find this easier to remember instead of all the backwhacking and brackets.

    eg.

    cam@XX:/tmp/tmp> find -type f | xargs grep here
    ./text1:searchstring is here
    ./text2:searchstring is also here

  3. f241vc15 Says:

    Yep that is faster, but using ‘find’ the way I used it has its benefits/upsides as well (see edited post above)

  4. Online Games,Search For Files SWF GAMES PLAY Says:

    Online Games,Search For Files SWF GAMES PLAY…

    […]Quick and cool Linux commands! « F241vc15' Weblog[…]…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: