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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...