nl_command
stringlengths
14
387
bash_code
stringlengths
0
532
find any files in the current directory that begin with a number
find . -regex './[0-9].*' -print
Go to last directory with name starting with a number, useful for timestamped directory names.
cd "$(ls -rd [0-9]*/ | tail --lines 1)"
find all the files in the current directory which have been modified in the last 24 hours
find . -mtime 0
search for the file picasso in the folder /home/calvin/
find /home/calvin/ -iname “picasso”
Find broken links
find / -type l -print0 | xargs -0 file | grep broken
change owner of the file "file" to user user_name
chown user_name file
display all files in the current folder with the name test excluding those that are present in the sub folders of the test folder
find . -name test -prune
Print the base name via grep of the current working directory
pwd | grep -o '[^/]*$'
Make directories to "$2" as needed
mkdir -p $2
Removes the last 2 lines from a file
head -n -2 myfile.txt
Find all files except files with '.gz' extension in the current directory non-recursively and compress them with gzip
find . -maxdepth 1 -type f ! -name '*.gz' -exec gzip "{}" \;
Find the total size of *.jpg files within the current directory tree
find . -type f -iname '*.jpg' -print0 | xargs -r0 du -a| awk '{sum+=$1} END {print sum}'
Remount "/" with read and write permission
mount / -o remount,rw
display all files in current folder with NULL separating each file
find . -print0
Count all the lines of all php files in current directory recursively
find -name '*.php' | xargs cat | wc -l
Search the files of the current directory tree for string "searched-string"
find . | xargs grep "searched-string"
run script "runProgram.sh" as user jetty
su - jetty sh ./runprogram.sh
Show manual page for the find utility
man find
Find all directories under current directory and run /path/to/script.sh for each of them
find . -type d -exec /path/to/script.sh '{}' \;
Replace all occurrences of edx with gurukul in all *.css files under ./cms/djangoapps/contentstore/views directory
find ./cms/djangoapps/contentstore/views -iname *.css | xargs sed -i s/[Ee][Dd][Xx]/gurukul/g
Installs locally located 'packagename.arch.rpm' package, ignoring GPG checking.
yum --nogpgcheck localinstall packagename.arch.rpm
Backup MySQL database "database" with "username" and "pswd", compress with gzip and add the time stamp before emailing to "[email protected]"
mysqldump -e --user=username --password=pswd database | gzip | uuencode my-dbbackup.`date +"\%Y-\%m-\%d"`.gz | mail [email protected]
Display the named characters in "line1\r\nline2"
echo -e "line1\r\nline2" | awk '{ print $0; }' | od -a
Find all files name passwd in the root directory and all its sub-directories.
find / -name passwd
Find all files/directories under current directory that match the case insensitive regex ./\(RT\|ED\).* and show several lines of output from the beginning
find . -iregex './\(RT\|ED\).*' | head
display files ending with ".ext" in current folder which are present in the file "foo"
find . -type f -name \*.ext | xargs grep foo
List all variables with names containing X.
env | awk -F "=" '{print $1}' | grep ".*X.*"
Search for 'foo=' in all *.png files under current directory without descending into *.gif and *.svn directories
find . -name "*.png" -prune -o -name "*.gif" -prune -o -name "*.svn" -prune -o -print0 | xargs -0 -I FILES grep -IR "foo=" FILES
Compare sorted files 'f1.txt' and 'f2.txt' and print in three columns strings unique for first file, second file, and common ones
comm < <
Look for all files whose names match pattern 'my*'
find / -name 'my*'
Finds recursively all folders in current folder which path not contains "NameToExclude" string and removes only ones without files and another folders within.
find . -type 'd' | grep -v "NameToExclude" | xargs rmdir
Find all directories under 'A' directory tree excluding paths containing the directory 'a'
find A -type d \
search for the file, filename.txt in the current folder ( case insensitive search )
find . -iname filename.txt
Find all the SUID files in the current directory tree
find . -perm /u=s
Extract and print hostname and protocol part from URL like "http://www.suepearson.co.uk/product/174/71/3816/".
echo "http://www.suepearson.co.uk/product/174/71/3816/" | cut -d'/' -f1-3
Search the current directory tree for regular files that contain "string"
find . -type f -printf '"%p"\n' | xargs grep string
change the owner of all the regular/normal files which belong to the root user to "tom" in the current folder
find .-type f -user root -exec chown tom {} \;
Recursively copy all files matching "*.sh" in "$from" to "root@$host:/home/tmp/" compressing data during transmission
rsync -zvr --exclude="*" --include="*.sh" --include="*/" "$from" root@$host:/home/tmp/
long list al the files in the current directory which have all the permissions
find . -perm 777 -type f -exec ls -l {} \;
Copy entire directory structure from dir1 as a subdirectory of dir2, omitting any file or directory named "exclude".
mkdir dir2; tar cvf - dir1/ --exclude "*/exclude" | tar xvf - -C dir2
Find all files under and below the current working directory with the word California in the file (case insensitive), and count the number of lines in the output
find . -type f -exec grep -i California {} \; -print | wc -l
Print current date as epoch seconds
date +%s
Search for 'pattern' in all files with '.cc' extension under current directory tree and show the matched lines with line numbers and filenames
find . -name “*.cc” |xargs grep -n “pattern”
Numerically sort file "table" by the fourth character of the second field, ignoring leading spaces
sort -b -n -k2.4 table
Find all directories named "nasa"
find . -name nasa -type d
display all files in current folder using regular expression
find -regex "$rx"
Find all directories under $ROOT_DIR and show the sub-directories of the directories before the directories themselves
find $ROOT_DIR -type d -depth -print
Create a rsa key with comment specified by variable APP and passphrase specified y SSHKEYPASS.
ssh-keygen -t rsa -C "$APP" -N "$SSHKEYPASS" -f ~/.ssh/id_rsa
Look up for 'myip.opendns.com' in server 'resolver1.opendns.com' and save the terse output in 'IP' variable
IP=$(dig +short myip.opendns.com @resolver1.opendns.com)
Recursively copy all files and directories in "demo" excluding ".git" to "demo_bkp"
find demo -depth -name .git -prune -o -print0 | cpio -0pdv --quiet demo_bkp
Find all files under media/ directory and change their permission to 600
find media/ -type f -exec chmod 600 {} \;
Find files under [directory] that match 'pattern_to_INCLUDE' in their names without descending into directories that match 'pattern_to_exclude' and 'another_pattern_to_exclude', then search for 'pattern' in those files
find [directory] -name "pattern_to_exclude" -prune -o -name "another_pattern_to_exclude" -prune -o -name "pattern_to_INCLUDE" -print0 | xargs -0 -I FILENAME grep -IR "pattern" FILENAME
display long listing of all normal/regular files in the current directory which have been modified in the last 2 days.
find . -mtime -2 -type f -name "t*" -exec ls -l '{}' \;
Find all files/directories under current directory tree that start with 'test' in their names without descending into directories with the same name pattern
find . -name 'test*' -prune
Change permissions of "/dvtcolorconvert.rb" to 755
sudo chmod 755 /dvtcolorconvert.rb
Find all 400 permission files under /data directory and change their permission to 755
find /data -type f -perm 400 -print | xargs chmod 755
sleep until a specific date
sleep $
Count the number of files in the /usr/ports directory tree whose names begin with 'pkg-plist' and which contain 'etc/rc.d/'
find /usr/ports/ -name pkg-plist\* -exec grep -l etc/rc.d/ '{}' '+' | wc -l
Print command history
history
Save the first "." separated field of the system host name to variable "HOSTZ"
HOSTZ=$( hostname | cut -d. -f1 )
Remove the last two alphanumeric file extensions from "mpc-1.0.1.tar.gz"
echo "mpc-1.0.1.tar.gz" | sed -r 's/\.[[:alnum:]]+\.[[:alnum:]]+$//'
Create a symbolic link to file "/usr/bin/perl" named with escaped characters "/usr/local/bin/perl\r\n"
sudo ln -s /usr/bin/perl /usr/local/bin/perl`echo -e '\r'`
Variable PID contains a process ID, send SIGTERM to this process if it exists.
kill $PID
Find all executables under /path directory
find /path -perm /ugo+x
Move all *.mp4 files from directory /foo/bar and its subdirectories to /some/path
find /foo/bar -name '*.mp4' -exec mv -t /some/path {} +
Find all files in current directory and search for 'searchName' in those files
find ./ -name "*" | xargs grep "searchName"
Find all directories under 'project' directory without going into subdirectories that do not match the POSIX egrep regex $PATTERN in their paths and are not empty
find project -maxdepth 1 -mindepth 1 -regextype posix-egrep ! -iregex $PATTERN ! -empty -type d
Display a tree-like view of USB devices, letting the user page through it with the arrow keys, with the Q key to quit.
sudo lsusb -t|less
Search the current directory recursively for files last modified within the past 24 hours ignoring .swp files and paths ./es* and ./en*
find -mtime 0 -not \
Find *.c and *.sh files
find . -type f \( -name "*.c" -o -name "*.sh" \)
Search the files from the current directory tree for "chrome"
find . | xargs grep 'chrome'
Archive "blanktest/" to "test/" deleting any files in the destination not found in the source
rsync -a --delete blanktest/ test/
Delete all regular files under $DIR directory tree that have been modified before file $a
find "$DIR" -type f \! -newer "$a" -exec rm {} +
List all files in the current directory tree that were modified less than 60 minutes ago
find . -mmin -60 |xargs ls -l
List all files/directories under current directory matching the posix-egrep type regex ".+\.$" in their names excluding the paths */generated/* and */deploy/*
find . -regextype posix-egrep -regex '.+\.$' -not -path '*/generated/*' -not -path '*/deploy/*' -print0 | xargs -0 ls -L1d
Print a NULL-separated list of all directories of the current directory tree
find . -type d -print0
Change permissions to 644 for all files showing the respective chmod command
find ./ -type f -print0 | xargs -t -0 chmod -v 644
Search for 'ireg' in all PHP files under 'project' directory tree and show only the files that match
find project -name '*.php' -type f -print0 | xargs -0 grep -l ireg
find all the files in the entire file system that have been modified exactly 7 days before which end with "conf"
find / -name "*conf" -mtime 7
Find all symlinks under /path/to/dir and replace them with the actual files they are pointing at
find /path/to/dir -type l -exec sh -c 'cp --remove-destination "$" "{}"' \;
Locates Erlang distribution, and prints release number from 'RELEASES' file.
awk -F, 'NR==1 {gsub;print $3}' "$(dirname $(readlink -f $))/../releases/RELEASES"
Copy "some_file_name" to "destination_directory" and change ownership to "someuser:somegroup"
echo 'some_file_name' | cpio -p --owner someuser:somegroup destination_directory
search for a word in all the regular files in the current folder.
find -type f -print0 | xargs -r0 grep -F 'example'
Adjust the timestamp of file $filename by subtracting 2 hours from it
touch -d "$(date -r "$filename") - 2 hours" "$filename"
find all files that names are filename and open in nano text editor
find / -name filename -exec nano '{}' \;
Print command history
history
remove all the DS_Store files in the current directory
find . -name .DS_Store -exec rm {} \;
List all of the subdirectories in the current directory with no trailing slash.
ls -d */ | cut -f1 -d'/'
Find all regular files under /home/www and replace every occurrences of 'subdomainA.example.com' with 'subdomainB.example.com' in those files invoking sed as few times as possible
find /home/www/ -type f -exec sed -i 's/subdomainA\.example\.com/subdomainB.example.com/g' {} +
List the unique second "/" delimited field of every line from standard input prefixed by the number of occurrences
cut -d/ -f1-2 | cut -d/ -f2- | sort | uniq -c
Find all files/directories named Root under current directory and copy them to newRoot
find . -name Root | xargs cp newRoot
Recursively finds in all folders but "./output/*" all files but *.o, *.swp and prints strings with 'soc_attach' text pattern and number of matched string.
find . \ -a \ -a \ -a \ | xargs grep -n soc_attach
Find all files under the current directory and copy their permissions to the same file in "../version1"
find . -type f | xargs -I {} chmod --reference {} ../version1/{}
Find all the files that are not named "MyCProgram.c" in the current directory only and without regards to case.
find -maxdepth 1 -not -iname "MyCProgram.c"
Change every file under "/var/www/html/" to have permissions 664
sudo find /var/www/html/ -type f -exec chmod 664 {} \;
Read a single character from standard input into variable "key" without backslash escapes and using the prompt "Press any key to continue..."
read -n1 -r -p "Press any key to continue..." key
Print a listing of the `other' directory
find other -maxdepth 1 -printf "%P\n"
Recursively changes group ownership of every folder in a current directory to the name of that folder.
find . -type d | sed -e 's/^\.\///g' | awk '{print $1, $1}' | xargs chgrp
Finds $a pattern in a $b string, and returns exit code 0 if found, suppressing any visible output.
echo $b|grep -q $a
Execute "myscript.rb" with the first argument as the current host name and the second "file.txt"
myscript.rb `hostname -i` file.txt