3

How can I rename oddly named directories without typing their full names?

I've got 8 folders with very long foldernames and I only got these folders in this path /home/user/Downloads/ - Is there any command to get every {*.txt} file from all folders and move them to another directory?

2
  • This question is very hard to make sense of. Edit your question to tell us what you have. Then tell us what you want. We may be able to answer that question.
    – msw
    Apr 13, 2011 at 11:22
  • see - I got 8 folders with very long foldernames and I only got these folders in this path /home/user/Downloads/ - I wanted to ask if there is any command to get every txt file of ALL folders and move them to another directory ?!
    – user14166
    Apr 13, 2011 at 11:35

4 Answers 4

5

If I rewrote your question properly based on your comment then

find /home/user/Downloads -type f -name \*.txt -print0 | \
  xargs -0  -I '{}' mv '{}' nice-directory

will reliably do what you ask.

1
  • If you'd use xargs -0 mv -t nice-directory, you would save on mv executions.
    – pLumo
    Feb 1, 2022 at 7:50
1

You could just:

mv firstCoupleLetters[tab] newDir[enter] 

and rely on autocomplete to fill out the original directory name, renaming it to something shorter/more useful.

1

To rename a directory like horrible *[name]* with$weird <characters> \\in it, you can use wildcards. For example, if you have only one directory with weird in the name:

mv "*weird*" a-regular-named-directory

where the " are important. If this fails, you can use

ls --hide-control-chars

That will give you a listing with odd characters replaced by ?, which is then a shell pattern upon which you can use mv.

0

msw's answer using xargs is OK.

However, I would do

  1. with find -exec instead of xargs.
  2. using mv -t target sources... form instead of mv source target, so you don't need to call mv for each file.
find ~/Downloads -type f -name "*.txt" -exec mv -t "/path/to/other_directory" {} +

Alternatively, you can use globstar:

shopt -s globstar
mv ~/Downloads/**/*.txt "/path/to/other_directory"

# Unset globstar again if you don't need it anymore:
shopt -u globstar

You must log in to answer this question.