Showing posts with label GNU/Linux. Show all posts
Showing posts with label GNU/Linux. Show all posts

Sunday, 26 February 2012

BASH - result of remote command into local variable

If you like to code in BASH this tip may be interesing for you. It is possible to call a remote command in BASH for example through SSH and put it's result into a local variable. For example you can run your script by CRON on local machine and login to remote server through SSH to check if some file appears in given directory.

It's other problem to choose a login method - by pass entered into script code or by chenge of SSH certificates keys - better is to not put password into script because of security reasons. 


To put result of remote command into a local variable in BASH use this construction:

result=$(ssh login@host remote_command)
For example:
result=$(ssh login@host ls -all|grep searched_file.txt)


Article on CC-BY-SA-3.0

Wednesday, 24 August 2011

Recursive count lines number in large programming projects.

To count lines number from for example all *.java files in directory there is simple solution with "wc" command.

wc -l *.java

If you need to count lines number of large project with subdirectories etc. you can use
combination of "wc" and "find" command:

wc -l `find . -name \*.java -print`

To many arguments for mv / cp.

If you have to copy large number of files from one directory to another from console level (for example trough ssh) and you see message like "Too many arguments to proceed" there are few solutions. The best in my opinion is these using "find" command.

find . -iname "*.EXT" -exec cp {} -l /DST_PATH/ \;
Where EXT is extension of files you want to copy. If you want to copy all files set it to "*" . DST_PATH is destination path where you want to copy files.

You have to call this command from source path, so af first change your current directory to source by "cd" command.

If you want to copy all files from current directory but not from subdirectories here is solution with -maxdepth parameter.

find . -maxdepth 1 -iname "*.EXT" -exec cp {} -l /DST_PATH/ \;

 Good luck :)

Saturday, 5 March 2011

Use tar.gz on GNU/Linux

To decompress a tar.gz archive use this syntax from command line level:
gzip -dc file_name.tar.gz | tar xf -

If you want to add files to tar archive, use this syntax:
tar -cvf file_name.tar file_to_compress
if you need to use gzip to compress tar archive use additional 'z' parameter:
tar -cvfz file_name.tar.gz file_to_compress


an example:
tar -cvf archive.tar ./directory/*

Sunday, 20 February 2011

How to merge files on Linux from console level

If someone sent you a parted file (for example a 7zip parted archive) you will need to merge these files before use (for example before extract a 7zip archive). On GNU/Linux operating system it is very simple. To do that you can use a 'cat' command and redirect of default output stream.

cat filename.part001 >> filename;
cat filename.part002 >> filename;
.......
cat filename.partN >> filename;

You should use double '>' sign to avoid overriding data. Single '>' sign means to delete content of file (here 'filename' ) and to put in data from filename.partN file. Double '>' will concatenate present content of file with data from filename.partN.

Good luck :)

Some rights reserved - CC-BY-SA-3.0 License.