Useful things to do with bash....


Useful copy command for backing up

Don’t like backup programs (apart from rsync), but I do like cp. Here is an easy way of only copying over changed or new files:

cp -urvp /path/to/source/* /path/to/target/

What does it all do?

cp the copy command

-u update only new or changed files with a newer date

-r recursive to all sub directories

-v verbose, tell me what is happening

-p preserve file attributes

Multi-Part Archive using 7z and CLI

Sometimes simple is better. File Roller wouldn’t create a multipart no matter what i tried so I turned to cli for a solution, and found it with 7z. In this case I need to archive a 1.2GB iso file to a four part archive, with a volume size of 300mb. I didn’t delve into all the intricacies of what 7z can do, just the basics.

7z a -t7z -v300M output.7z input.iso

This produced four files:






7z: calls the program

a: creates an archive

-t7z: selects 7z as the archive type (there are others)

-v300M: tells 7z to create 300mb archive parts

output and input files should be obvious. You could replace the input file with a directory!

man 7z

will give more detail on options

Run programs without sudo ?

Wouldn’t normally do this, or recommend it, but for occasional and considered use this is a useful tip. You need to run a program normally requiring sudo, but as a normal user. The usual way of fixing this is to put a line in /etc/sudoers, but you still need to type sudo. The problem I had was with a custom iso I have been putting together. Because my WM was openbox I was using a bash script with zenity to provide reboot and shutdown. This was all set up fine, but on installing the distro from the live cd, the installation needed to add a line to the bottom of /etc/sudoers, thereby negating my previously entered line.A long trawl through google provided an answer on ubuntuforums, which was to change the set uid bit for the shutdown command, so that all users could run shutdown with needing sudo. As you can see, doing this to a more important command would create security holes, but in this instance there is not much a hacker can do with shutdown, other than shutdown…AFAIK !! So here is the command I used:

sudo chmod a+s /sbin/shutdown

which gives the following permission:

645 -rwsr-sr-x 1 root root 46864 2011-01-22 01:58 shutdown

I edited out the sudo shutdown in my script and replaced it with /sbin/shutdown and all is well.

There is another way to do this, perhaps best done inside a bash script:

echo "yourpassword" | sudo -S "program"

This works well in most cases.

Run Once Only Bash Scripts

I am constantly amazed by what can be achieved with a simple bash script or two. I had a situation where I needed a script to run only once, and to not get any errors spat out at me from the cli. I needed a second script to call the run once script to go into rc.local, a file I didn’t want to mess with, so the call in rc.local would just be a quick file check after the first run. for me, the clever bit is that the script. I have to do the work can “move” (rename) itself once it has done its business, thereby ensuring it is not called upon again, but is still there if needed.

Here’s the script that does the work (called


Do the work

echo “I am now going to back myself up :)!”

sleep 2


echo file backed up

sleep 2

exit 0

The key line is the mv command, running on itself! I’ll remove the comments for the production setting

Here’s is the script that calls the one above (called


if [ -f ~/ ]


echo the file exists

sleep 2



echo the file does not exist

sleep 2


Basically, if is there, this script,, runs, which when finished, renames itself. If is not there, the script just finishes. Subsequent runs of the script called in rc.local just run through as there is no file to run. Simples 🙂