9

I had a directory named myname which was archived in myname.tar. Now I want to take myname.tar, unarchive it, and delete all the files in the myname directory (after unarchiving).

Here are my commands for the above task:

find -name deleteme.tar
tar -xvf deleteme.tar
rm -r delete/*

These individual commands are working fine, but I need a one-line command, so I tried this:

find -name deleteme.tar | tar -xvf deleteme.at | rm -r delete/*

I got this error:

"rm -r deleteme/*
rm: cannot remove `deleteme/*': No such file or directory "

What am I doing wrong?

3
  • 6
    Try replacing your | symbols with ; instead: find -name deleteme.tar; tar -xvf deleteme.tar; rm -r delete/* Commented Sep 10, 2012 at 20:31
  • 1
    The command sequence doesn't make a whole lot of sense to me. It reads like: find tar file, extract tar file, delete extracted contents. Is the purpose to exercise the hard disk?
    – jw013
    Commented Sep 10, 2012 at 21:36
  • I get the impression the OP is looking for something like find ... -exec and not something as simple as ;.
    – jw013
    Commented Sep 10, 2012 at 21:36

2 Answers 2

29

The symbols you are looking for are called control operators. You are using the wrong ones - you need ; or &&.

With ;, commands are executed in order, and it does not matter if the previous one ended with success or not.

With &&, every command that fails will prevent the next ones from executing.

You can read more for example in 8.1. control operators

1
7

In case you're using bash, may I suggest you read the man page of bash (man bash), and search for "control operator" (/control operator + [Enter]).

There, the different functions of things like | and ; are explained.

You might want to look into using && instead of ;, though. If you do that, you can perform the following functions in that sequence only if the previous one was successful. Meaning, if find cannot find your file, it wouldn't try to compress nothing:

find -name deleteme.tar && tar xvf deleteme.tar && rm -r delete/* || echo "something went wrong" 1>&2

Just a minor suggestion, though...

You must log in to answer this question.