norman's blog

Notes of an amnesiac.
Never stop thinking.
Find an aesthetic description.

Wednesday, May 13, 2009

BASH COMMANDS

ls--list files and folders in a directory (hierarchy)
-R: list subdirectories recursively
-a: do not ignore entries starting with.
-t: sort by time
-v: Interpret the digits in names such as file.6 and file.6.1 as versions, and order filenames by version.
find--search files in a directory hierarchy
search for files in a directory hierarchy, and relative paths of all found files are listed.
The syntax looks like this:
find where-to-look criteria what-to-do
Examples:
#find files named with suffix "cpp"                           
#in the current directory(max depth = 2) and delete them.
find . -maxdepth 2 -name "*.cpp" -delete 

# mv files older then 1 day to dir TMP
find . -atime +1 -type f -exec mv {} TMP \; 
see reference
grep--search pattern in the content of input file(s)
grep searches pattern in the named input FILEs (or standard input if no files are named). By default, grep prints the matching lines.
xargs
Build and execute command lines from standard input.
Execute command (with any initial arguments), but read remaining arguments from standard input instead of specifying them directly. xargs passes these arguments in several bundles to command, allowing command to process more arguments than it could normally handle at once. The arguments are typically a long list of filenames (generated by ls or find, for example) that get passed to xargs via a pipe.
xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.
Because Unix filenames can contain blanks and newlines, this default behaviour is often problematic; filenames containing blanks and/or new‐lines are incorrectly processed by xargs. In these situations it is better to use the ‘-0’ option, which prevents such problems. When using this option you will need to ensure that the program which produces the input for xargs also uses a null character as a separator. If that program is GNU find for example, the ‘-print0’ option does this for you.
#grep for pattern in the content of all files on the system:
find / | xargs grep pattern

#grep for pattern in the name list of all files on the system:
find / | grep pattern
Sed
stream editor for filtering and transforming text
ifconfig
dhclient
configure network interfaces using the DHCP protocol


2>/dev/null is not related to find tool as such. 2 indicates the error stream in Linux, and /dev/null is the device where anything you send simply disappears. So 2>/dev/null in this case means that while finding for the files, in case any error messages pop up simply send them to /dev/null i.e. simply discard all error messages.

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home