Commands matching search replace (67)

  • recursively traverse the directory structure from . down, look for string "oldstring" in all files, and replace it with "newstring", wherever found also: grep -rl oldstring . |xargs perl -pi~ -e 's/oldstring/newstring'


    25
    $ grep -rl oldstring . |xargs sed -i -e 's/oldstring/newstring/'
    netfortius · 2009-03-03 20:10:19 26
  • This command find all files in the current dir and subdirs, and replace all occurances of "oldstring" in every file with "newstring".


    16
    find . -type f -exec sed -i s/oldstring/newstring/g {} +
    SlimG · 2009-12-09 00:46:13 15
  • the addition of ".bk" to the regular "pie" idiom makes perl create a backup of every file with the extension ".bk", in case it b0rks something and you want it back


    10
    perl -pi.bk -e's/foo/bar/g' file1 file2 fileN
    xsawyerx · 2009-01-29 09:51:11 94
  • I needed a way to search all files in a web directory that contained a certain string, and replace that string with another string. In the example, I am searching for "askapache" and replacing that string with "htaccess". I wanted this to happen as a cron job, and it was important that this happened as fast as possible while at the same time not hogging the CPU since the machine is a server. So this script uses the nice command to run the sh shell with the command, which makes the whole thing run with priority 19, meaning it won't hog CPU processing. And the -P5 option to the xargs command means it will run 5 separate grep and sed processes simultaneously, so this is much much faster than running a single grep or sed. You may want to do -P0 which is unlimited if you aren't worried about too many processes or if you don't have to deal with process killers in the bg. Also, the -m1 command to grep means stop grepping this file for matches after the first match, which also saves time. Show Sample Output


    10
    sh -c 'S=askapache R=htaccess; find . -mount -type f|xargs -P5 -iFF grep -l -m1 "$S" FF|xargs -P5 -iFF sed -i -e "s%${S}%${R}%g" FF'
    AskApache · 2009-10-02 05:03:10 7
  • This microscript looks up a man page for each word possible, and if the correct page is not found, uses w3m and Google's "I'm feeling lucky" to output a first possible result. This script was made as a result of an idea on a popular Linux forum, where users often send other people to RTFM by saying something like "man backup" or "man ubuntu one". To make this script replace the usual man command, save it as ".man.sh" in your home folder and add the following string to the end of your .bashrc file: alias man='~/.man.sh' Show Sample Output


    9
    /usr/bin/man $* || w3m -dump http://google.com/search?q="$*"\&btnI | less
    d1337r · 2010-10-05 13:51:39 6
  • - grep for the word in a files, use recursion (to find files in sub directories), and list only file matches -| xargs passes the results from the grep command to sed -sed -i uses a regular expression (regex) to evaluate the change: s (search) / search word / target word / g (global replace)


    5
    grep -lr -e '<oldword>' * | xargs sed -i 's/<oldword>/<newword>/g'
    o0110o · 2011-08-27 02:42:51 7
  • Search for the string "search" and replace it with the string "replace", on all files with the extension php in the curret folder. Do also a backup of each file with the extension "bkp".


    4
    ruby -i.bkp -pe "gsub(/search/, 'replace')" *.php
    gustavgans · 2009-06-16 12:35:40 6
  • Only tested on Linux Ubunty Hardy. Works when file names have spaces. The "-maxdepth 2" limits the find search to the current directory and the next one deeper in this example. This was faster on my system because find was searching every directory before the current directory without the -maxdepth option. Almost as fast as locate when used as above. Must use double quotes around pattern to handle spaces in file names. -print0 is used in combination with xargs -0. Those are zeros not "O"s. For xargs, -I is used to replace the following "{}" with the incoming file-list items from find. Echo just prints to the command line what is happening with mv. mv needs "{}" again so it knows what you are moving from. Then end with the move destination. Some other versions may only require one "{}" in the move command and not after the -I, however this is what worked for me on Ubuntu 8.04. Some like to use -type f in the find command to limit the type. Show Sample Output


    4
    find . -maxdepth 2 -name "*somepattern" -print0 | xargs -0 -I "{}" echo mv "{}" /destination/path
    jonasrullo · 2009-08-01 01:55:47 7
  • xargs deals badly with special characters (such as space, ' and "). To see the problem try this: touch important_file touch 'not important_file' ls not* | xargs rm Parallel https://savannah.nongnu.org/projects/parallel/ does not have this problem.


    4
    grep -rl oldstring . | parallel sed -i -e 's/oldstring/newstring/'
    unixmonkey8046 · 2010-01-28 08:44:16 6
  • This one-liner greps first 30 direct URLs for .torrent files matching your search querry, ordered by number of seeds (descending; determined by the second number after your querry, in this case 7; for other options just check the site via your favorite web-browser). You don't have to care about grepping the torrent names as well, because they are already included in the .torrent URL (except for spaces and some other characters replaced by underscores, but still human-readable). Be sure to have some http://isup.me/ macro handy (someone often kicks the ethernet cables out of their servers ;) ). I've also coded a more user-friendly ash (should be BASH compatible) script, which also lists the total size of download and number of seeds/peers (available at http://saironiq.blogspot.com/2011/04/my-shell-scripts-4-thepiratebayorg.html - may need some tweaking, as it was written for a router running OpenWrt and transmission). Happy downloading!


    4
    wget -U Mozilla -qO - "http://thepiratebay.org/search/your_querry_here/0/7/0" | grep -o 'http\:\/\/torrents\.thepiratebay\.org\/.*\.torrent'
    sairon · 2011-04-15 15:01:16 30
  • Replaces A with B in binary file "orig" and saves the result to "new". You must have the hex representations of A & B. Try od: echo -e "A\c" | od -An -x


    3
    xxd < orig | sed 's/A/B/' | sed 's/HEXA/HEXB/' | xxd -r > new
    wwest4 · 2009-02-05 20:25:04 60

  • 3
    perl -i'.bak' -pe 's/old/new/g' <filename>
    unixmonkey5248 · 2009-08-25 17:37:59 4
  • sed already has an option for editing files in place and making backup copies of the old file. -i will edit a file in place and if you give it an argument, it will make a backup file using that string as an extension.


    3
    sed -i.bak 's/old/new/g' file
    deltaray · 2010-01-06 17:04:05 3
  • If you can install rpl it's simpler to use and faster than combinations of find, grep and sed. See man rpl for various options. time on above operation: real 0m0.862s, user 0m0.548s, sys 0m0.180s using find + sed: real 0m3.546s, user 0m1.752s, sys 0m1.580s Show Sample Output


    3
    $rpl -R oldstring newstring folder
    johnraff · 2009-12-09 03:15:31 9
  • Uses vi style search / replace in bash to rename files. Works with regex's too (I use the following a script to fixup / shorten file names): # Remove complete parenthetical/bracket/brace phrases rename 's/\(.*\)//g' * rename 's/\[.*\]//g' * rename 's/\{.*\}//g' * Show Sample Output


    3
    rename 's/foo/bar/g' foobar
    unixmonkey11428 · 2010-08-19 03:33:13 10
  • Note: Replace 200000 with drive bytes/512, and /dev/sdx with the destination drive/partition. ;) Note: You may need to install pipebench, this is easy with "sudo apt-get install pipebench" on Ubuntu. The reason I hunted around for the pieces to make up this command is that I wanted to specifically flip all of the bits on a new HDD, before running an Extended SMART Self-Test (actually, the second pass, as I've already done one while factory-zeroed) to ensure there are no physical faults waiting to compromise my valuable data. There were several sites that came up in a Google search which had a zero-fill command with progress indicator, and one or two with a fill-with-ones command, but none that I could find with these two things combined (I had to shuffle around the dd command(s) to get this to happen without wasting speed on an md5sum as well). For reference, these are the other useful-looking commands I found in my search: Zero-fill drive "/dev/sdx", with progress indicator and md5 verification (run sudo fdisk -l to get total disk bytes, then divide by 512 and enter the resulting value into this command for a full wipe) dd if=/dev/zero bs=512 count=<size/512> | pipebench | sudo tee /dev/sdx | md5sum And this command for creating a file filled with ones is my other main source (besides the above command and man pages, that is - I may be a Linux newbie but I do read!): tr '\000' '\377' < /dev/zero | dd of=allones bs=1024 count=2k Hope someone finds this useful! :) Cheers, - Gliktch Show Sample Output


    3
    tr '\000' '\377' < /dev/zero | dd bs=512 count=200000 status=noxfer | pipebench | sudo dd of=/dev/sdx
    Gliktch · 2010-08-31 15:38:27 6
  • Finds the string in every file in an entire directory and all its subdirectories and replaces it with a new string. Especially useful when changing a machine's IP address or hostname - run it on /etc.


    2
    perl -pi -e's/<what to find>/<what to replace it with>/g' `grep -Rl <what to find> /<dir>/*`
    adampbell · 2009-02-26 19:14:39 8
  • In this example we search for 'vim' but vim doesn't have a project on github right now. That's ok, this command still searches for every project that has 'vim' in their description (forks, plugins, etc). To get XML or JSON output just replace 'yaml' in the url with 'xml' or 'json'. Show Sample Output


    2
    curl http://github.com/api/v1/yaml/search/vim
    rkulla · 2010-05-30 00:29:03 3
  • Simple and easy. No regex, no search and replace. Just clean, built-in tools.


    2
    ifconfig eth0 | grep 'inet addr' | cut -d ':' -f 2 | cut -d ' ' -f 1
    atoponce · 2010-06-26 22:36:21 3
  • This function does a batch edition of all OOO3 Writer files in current directory. It uses sed to search a FOO pattern into body text of each file, then replace it to foo pattern (only the first match) . I did it because I've some hundreds of OOO3 Writer files where I did need to edit one word in each ones and open up each file in OOO3 gui wasn't an option. Usage: bsro3 FOO foo


    2
    bsro3 () { P=`pwd`; S=$1; R=$2; ls *.odt > /dev/null 2>&1; if [[ $? -ne 0 ]]; then exit 1; fi; for i in *.odt; do mkdir ${P}/T; cd ${P}/T; unzip -qq "$P"/"$i"; sed -i "s/$S/$R/" ${P}/T/content.xml; zip -qq -r "$P"/"$i" *; cd ${P}; rm -rf ${P}/T; done; }
    danpos · 2010-06-30 04:43:54 7
  • Searches Google, but requires no "", and will also search all terms input in the CL, eg: > google foo bar returns search URL " You could also use awk to replace all spaces with a +, which is how the Google search handles spaces, but that makes it more than one line.


    2
    function google () { st="$@"; open "http://www.google.com/search?q=${st}"; }
    plasticphyte · 2014-05-07 03:14:05 23
  • Replace all instances of "A" with "B" in file "source" saved as file "destination". !! IF A/B is multi-byte, then separate bytes with spaces like so: "s/20\ 0A/00/g". Show Sample Output


    2
    xxd -p source | fold -w2 | paste -sd' ' | sed "s/A/B/g" | xxd -p -r > destination
    hincor · 2015-05-26 18:29:48 24
  • Does an in situ search-replace but leaves a datestamped backup. A variation with more precision: sed -i.`date +%Y%m%d%H%M%S 's/pattern/replace' [filename]


    1
    sed -i.`date +%Y%m%d` -e 's/pattern/replace' [filename]
    wwest4 · 2009-02-05 18:20:54 31

  • 1
    find "$DIR" -regex "$FILENAME" -type f -print0 | xargs -0 sed -i _`date "+%y%m%d%H%M%S"` -E "s/$TEXT1/$TEXT2/g"
    unixmonkey813 · 2009-02-15 09:36:51 8
  • This does the following: 1 - Search recursively for files whose names match REGEX_A 2 - From this list exclude files whose names match REGEX_B 3 - Open this as a group in textmate (in the sidebar) And now you can use Command+Shift+F to use textmate own find and replace on this particular group of files. For advanced regex in the first expression you can use -regextype posix-egrep like this: mate - `find * -type f -regextype posix-egrep -regex 'REGEX_A' | grep -v -E 'REGEX_B'` Warning: this is not ment to open files or folders with space os special characters in the filename. If anyone knows a solution to that, tell me so I can fix the line.


    1
    mate - `find * -type f -regex 'REGEX_A' | grep -v -E 'REGEX_B'`
    irae · 2009-08-12 22:24:08 3
  •  1 2 3 > 

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands


Check These Out

Look for English words in /dev/urandom
* to get the English dictionary: wget http://www.mavi1.org/web_security/wordlists/webster-dictionary.txt

Make Kali Linux look less suspicious by making the desktop look more like a windows machine
To revert back to Kali's original desktop. Just redo the same command with no options .

HTTP GET request on wireshark remotly

edit hex mode in vim
return to normal mode from hex mode :%!xxd -r

Check if it's your binary birthday!
Print out your age in days in binary. Today's my binary birthday, I'm 2^14 days old :-) . This command does bash arithmatic $(( )) on two dates: Today: $(date +%s) Date of birth: $(date +%s -d YYYY-MM-DD) The dates are expressed as the number of seconds since the Unix epoch (Jan 1970), so we devide the difference by 86400 (seconds per day). . Finally we pipe "obase=2; DAYS-OLD" into bc to convert to binary. (obase == output base)

diff the same file in two directories.
This is useful when you're diffing two files of the same name in radically different directory trees. For example: Set $ path1='/some/long/convoluted/path/to/all/of/your/source/from/a/long/dead/machine' then $ path2='/local/version/of/same/file' then run the command. Much easier on the eyes when you're looking back across your command history, especially if you're doing the same diff over and over again.

Belgian banking "structured communication"
Derived from current time down to minutes.

List detailed information about a ZIP archive
list zipfile info in long Unix ``ls -l'' format.

draw line separator (using knoppix5 idea)

Convert CSV to JSON
Replace 'csv_file.csv' with your filename.


Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: