Title: Mac terminal: Deleting files from a folder whose name is equal to files in another folder
Tags: macos;bash;shell;terminal
Question: The layout of the two directories in question is:
folder1
-file1
-file2
folder2
-file1
-file2
The files in the directories are named the same. I tried to use rm to programmatically remove files in folder 2:
for f in /Users/michelegiarratana/1/*; do rm /Users/michelegiarratana/2/$f
but this isn't working. Is $f the full path? How do I get the name?
Thanks
Comment: Use `basename`: `for f in /path/to/folder/1/*; do rm "/path/to/folder/2/$(basename "${f}")"; done`

Comment: That probably means that there are no files in `/Users/michelegiarratana/1/`.

Comment: The `*` character gets expanded by `bash` to "all files in the directory". If there are files under /Users/michelegiarrantana/ **1** /, then the `*` will get replaced by those file names. However, your error message is `rm: /Users/michelegiarratana/2/*: No such file or directory`. This happens when there are no files for `*` to expand. In this case, it gets treated as a literal `*` character, ie. `basename $f == '*'`. So you are literally running `rm /Users/michelegiarratana/2/*`.

Comment: In that case I don't understand either. Sorry, I'm out of ideas.

Comment: It still sees me rm: /Users/michelegiarratana/2/*: No such file or directory

Comment: no, both have the same files inside

Comment: you are right but the folder does actually contains files...so I don't understand...for f in /Users/michelegiarratana/1/*; do rm /Users/michelegiarratana/2/$(basename "${f}"); done; should make it


Here is another answer: As savanto said in his comment -- yes, you are correct, $f here is the full file path. To fix this, you should use the basename builtin, which extracts just the file name. In your example:
for f in /Users/michelegiarratana/1/*; do rm /Users/michelegiarratana/2/$(basename $f); done
Additionally -- your question applies more broadly to bash shells than just the OS X terminal, in the future, you should try looking for answers in the bash tag, as many of them will be useful to you.
Comment for this answer: My preference would be to use Bash's substring capabilities, rather than calling basename (which I don't believe is actually a builtin, per http://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html). So I'd replace `$(basename)` with `${f##*/}`.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 7, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:06/03/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<macos><bash><shell><terminal>', 'Title': 'Mac terminal: Deleting files from a folder whose name is equal to files in another folder', 'ViewCount': 136, 'id': 24005676, 'user_AboutMe': '', 'user_AccountId': 274016.0, 'user_DisplayName': 'Michele', 'user_Reputation': 681.0, 'user_Views': 179.0, 'user_id': 566001.0}}