1

I am running into the problem of commands failing because I expect them to be executed at some directory and that is not the case.

For example I want to do:

pdfcrop --margins '0 0 -390 0' $pag "$pag"_1stCol.pdf

to create a new pdf document, and then

mv `\ls /home/dir | grep '_1stCol'` /home/gmanglano/dir/columns

The problem is that the mv command is failing because it finds the document, it is trying to move that file found FROM the directory where I executed the script, not from where it was found.

This is happening to me somewhat often and I feel there is a concept I am missing or I am thinking this the wrong way arround.

The error I get is:

mv: cannot stat '1stCol.pdf': No such file or directory

When there is, in fact, said fail, it just is not in the directory I launched the script.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
monkey intern
  • 705
  • 3
  • 14
  • 34
  • 1
    BTW, better if you quote both expansions of `$pag` -- as in, `pdfcrop --margins '0 0 -390 0' "$pag" "${pag}_1stCol.pdf"` -- without quoting the first one, it can be split into any number of separate `pdfcrop` arguments if it contains spaces, wildcards, etc (or is empty, in which case it can become *zero* arguments). – Charles Duffy Sep 25 '17 at 14:37
  • 1
    And there's no need for `\ls` here (though you shouldn't be using `ls` noninteractively anyhow) -- there are no aliases in scripts unless you explicitly do work to turn them on and define them (they're off-by-default in noninteractive shells). – Charles Duffy Sep 25 '17 at 14:38

2 Answers2

4

Instead of monkeying with ls and backticks and all that, just use the find command. It's built for to find files and then execute a command based on the results of that find:

 find /home/dir -name "*_1stCol.pdf" -exec mv {} /home/gmanglano/dir/columns \;

This is finding files in /home/dir that match the name *_1stCol.pdf and then moves them. The {} is the token for the found file.

JNevill
  • 46,980
  • 4
  • 38
  • 63
  • What about when I want to do a negative search? I've been going back and forth with using grep and find for this task and in this case I ended up using **mv `\ls /home/dir | grep -xv "pdf.pdf"` /home/dir2** and that is the reason to be doing it so weirdly. Any ideas? I wasn't able to find out how to do it with find – monkey intern Sep 25 '17 at 14:53
  • I think `find` is still a good solution. [check out this question](https://stackoverflow.com/questions/1341467/unix-find-for-finding-file-names-not-ending-in-specific-extensions) for using the `-not` flag in `find`. It's a horribly versatile tool. – JNevill Sep 25 '17 at 14:56
  • But that is looking for a regex. With **grep -xv**, I am able to match a exact name. Sorry if I am overcomplicating the issue. – monkey intern Sep 25 '17 at 15:00
2

Don't parse the output of ls: if you simplify the mv command to

mv /home/dir/*_1stCol.pdf /home/gmanglano/dir/columns

then you won't have an issue with being in the wrong directory.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578