Tuesday, October 30, 2012

The XARGS command


The xargs Command.


Xargs' power lies in the fact that it can take the output of one command, and use that output as arguments to another command. So, using the basic find command below, let us pass the output of find to xargs and get xargs to issue multiple 'ls -l' commands.

# find ~ -type f -mmin -90 | xargs ls -l

Sample output: 
-rw-rw-r--    1 linux    lunux    10209032 Jun 30 13:28 /home/linux/dns/mysql-outfile/dnsx.doc
-rw-rw-rw-    1 mysql    mysql    10209032 Jun 30 12:53 /home/linux/dns/mysql-outfile/dnsx.txt

Important Switches

-r --> Tells xargs to quit; when the its i/p doesn't contain any non-zero files.
# find ~ -type f -mtime +1825 |xargs -r ls -l

-0, --null --> Expect filenames to be terminated by NULL instead of whitespace. Do not treat quotes or backslashes specially.
Used with print0 command (at first set), so the args are terminated with a null.

$ find /share/media/mp3/ -type f -name "*.mp3" -print0 | xargs -0 -r -I {} cp -v -p {} --target-directory=/bakup/iscsi/mp3/

-n args, --max-args=args --> Max number or args to pass to the 2nd command at a time.
$ echo 1 2 3 4 | xargs -n 2

-I [string] --> Replace all occurrences of { }, or string, with the names read from standard input. Unquoted blanks are not considered argument terminators. Implies -x and -L 1.

-L [lines] , --max-lines[=lines] --> Allow no more than lines nonblank input lines on the command line (default is 1). Implies -x.
-x, --exit --> If the maximum size (as specified by -s) is exceeded, exit.
-s max, --max-chars=max --> Allow no more than max characters per command line.


Most used while deleting/copying a large number of files which can't by default, handled by rm or cp or any other commands.

Xargs examples.

# ls |xargs -I {} cp -v {} /mnt/import_log/


The above command will copy all the files from the current location to /mnt/import_log/ irrespective of number of files.


# ls |xargs -n10 -I {} tar -cvf /mnt/import_log_file.tar {}

The above command will tar the entire contents of the current directory to /mnt/import_log_file.tar.


Similarly you will find the xargs command to be useful in many cases.

An example error if you try to delete contents a directory with a lot of files.

/bin/rm: Argument list too long.

Just use the xargs as above to solve this.

Saturday, April 28, 2012

Mounting iSCSI Luns on Linux Hosts

Adding iSCSI Storage to a Linux Computer
 
This article describes mounting iSCSI luns from SAN iSCSI targets to Linux hosts. Please make sure that an iSCSI Target has been created and ready for use in theSAN / NAS targets. 


Step 1: Install the iSCSI Initiator Software

[root@test.com /]# yum install iscsi-initiator-utils
Loaded plugins: presto, refresh-packagekit
Setting up Install Process
Resolving Dependencies
Running transaction check
Package iscsi-initiator-utils.i686 0:6.2.0.872-7.fc13 set to be installed
Finished Dependency Resolution
...
Installed:
  iscsi-initiator-utils.i686 0:6.2.0.872-7.fc13                                

Complete!

Things you need to have before setting up the iSCSI mounts.
  1. iSCSI startup using the init script or manual startup. You need to edit and configure iSCSI via /etc/iscsi/iscsid.conf file
  2. Discover targets.
  3. Automate target logins for future system reboots.
  4. You also need to obtain iSCSI username, password and storage server IP address (target host)
 Step 2: Edit the iscsi.conf file

Setup username and password:
node.session.auth.username = My_ISCSI_USR_NAME
node.session.auth.password = MyPassword
discovery.sendtargets.auth.username = My_ISCSI_USR_NAME
discovery.sendtargets.auth.password = MyPassword


Where,
  • node.session.* is used to set a CHAP username and password for initiator authentication by the target(s).
  • discovery.sendtargets.* is used to set a discovery session CHAP username and password for the initiator authentication by the target(s)
Make sure iscsi.conf 'node' parameter is set for Automatic
[root@test.com /]# vi /etc/iscsi/iscsid.conf
node.startup = automatic

Now start the iscsi service:
# /etc/init.d/iscsi start

Friday, March 30, 2012

The Find command cheat sheet


The Find Command Cheat Sheet


1. Basic find command
# find -name "TestFile"

2. Find Files Using Name and Ignoring Case
# find -iname "TestFile"

3. Limit Search To Specific Directory Level Using mindepth and maxdepth
# find / -maxdepth 3 -name passwd
-maxdepth --> will go 3 directories below -- / 1st; /etc 2nd; /usr/bin 3rd

# find / -mindepth 3 -maxdepth 5 -name passwd
will go 3 depths first and upto 5 -- so will not disply under /; /usr; /usr/bin

4. Executing Commands on the Files Found by the Find Command.
user -exec {} /;
# find -iname "TestFile" -exec md5sum {} \;

5. Inverting the match.
To inver the match use the "-not" switch
# find / -not -iname "TestFile"

6. List inodes of the files
# ls -i1 test*
16187429 test-file-name
16187430 test-file-name

# find -inum 16187430 -exec mv {} new-test-file-name \;
# ls -i1 *test*
16187430 new-test-file-name
16187429 test-file-name

7. Find file based on the File-Permissions
You can :
    * Find files that match exact permission
    * Check whether the given permission matches, irrespective of other permission bits
    * Search by giving octal / symbolic representation

# find . -perm -g=r -type f -exec ls -l {} \;
Will display all files with group permission read. Not files with readonly group permission

# find . -perm g=r -type f -exec ls -l {} \;
Will dispay files with 040 permission. i.e files with group read only permisison

# find . -perm 040 -type f -exec ls -l {} \;
Will dispay files with 040 permission. i.e files with group read only permisison