Jump to content

bash nuggets


Steve Scrimpshire
 Share

Recommended Posts

I thought I'd start a topic to consolidate the knowledge I gain on/from bash (mainly from snippets of code that aru posts):

Replace text in files with bash:

~$ cat > foo
some text for the text example
some few more text
                                                                              
~$ while read lines; do echo "${lines//text/WORDS}"; done < foo
some WORDS for the WORDS example
some few more WORDS

~$

 

Renaming files with a space in the name (or "The amazing 'find' command"):

find . -type f -regex ".*\ .*" -exec bash -c 'mv "$1" "${1// /_}"' '{}' '{}'  \;

 

 

Converting files from upper case to lower case in a *single* directory:

~$ for file in *; do mv "${file}" "$(echo $file | tr 'A-Z' 'a-z')"; done

 

Recursively:

~$ find ./ -type f -exec bash -c 'f=$(basename "$1"); d=$(dirname "$1");  mv -i "$d/$f" "$d/$(echo $f|tr "A-Z" "a-z")"' {} {} \;

 

More to come as I find them...

Link to comment
Share on other sites

A little 'sed' snippet:

~$ cat > input_file
schema.current.name=it_current_username
schema.current.passwd=it_current_password

~$ sed 's/\(schema.current.name\)=.*/\1=it_new_name/g; s/\(schema.current.passwd\)=.*/\1=new_passwd/g'  input_file
schema.current.name=it_new_name
schema.current.passwd=new_passwd

~$

Link to comment
Share on other sites

Of course you have permission. DUH. Correct me if I post something wrong, too, because I like to do things 'by the book'....like always quoting variables containing filenames, etc.

Edited by Steve Scrimpshire
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...