0

This question already has an answer here:

Note: After asking this question, I found that the file browser Thunar, which comes with Xubuntu and is installable in any Ubuntu version, provides a bulk-renaming utility that works well for me.


I am trying to append timestamp hashes to files in directory or a userFileCode like a file password (for example all files of user have a prefix or suffix).

My problem is the mv command only works once so I cannot use wildcards like so

mv *.* *.*_dateHashsuffix

error says it's not a directory.

Seems like mv cannot handle multiple renames or do I have to use it like in cd /usr/bin && sudo mv test test_disabled && mv sudo sudo_disabled... in other words perhaps I have to pipe the ls > filelist.tst and then parse (do I use |grep here?) that separately in a .sh file script with executable permission run as so ./myscriptsname.sh or cant I use ls > directly in the script?

I see a use for this on my servers in the future to rename variables and files to prevent hacking. But a better idea altogether might be torrentDNS and zipSites in perhaps voxel based web browser with links to webpage content.

marked as duplicate by muru bash Oct 22 '18 at 11:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    I don't see a connection to Ubuntu in your question. You mention unix only, so a unix site may be more appropriate (eg. unix.stackexchange.com) however if it were me, I'd use rename (and the * alone means any number of characters including fullstop) – guiverc Oct 20 '18 at 11:59
  • 1
    You should avoid parsing ls at all times – Zanna Oct 20 '18 at 17:19
  • Is _dateHashsuffix constant for all files in one run or should it differ for each file? What are your actual needs? Doing cd /usr/bin && sudo mv <bulk_of_files> looks like playing with fire. – Melebius Oct 22 '18 at 7:53
4

You can not use wildcards in the destination of the mv-command. What you think are wildcards is interpreted as the first three characters of the destinations name.

mv *.* *.*_something

will try to rename any file with a dot in its filename in the current directory to the name *.*_something. This will be successful for the first file the shell finds. The second attempt will fail because the file *.*_something already exists, thus you get an error saying that the destination is not a directory.

You have to go another way:

for num in *.*; do mv "${num}" "${num}_something"; done
  • 1
    Please note that *.* is a common pattern in Windows and DOS but actually means: all filenames with an arbitrary number of characters, followed by a dot, followed by another arbitrary number of characters. So it will match e.g. readme.txt (because it has a dot in it) but not e.g. README (no dot). – PerlDuck Oct 20 '18 at 13:45
  • 2
    @PerlDuck That's what I wrote: any file with a dot in its filename. – mook765 Oct 20 '18 at 14:04
  • 1
    Oh, I stand corrected. I didn't notice that. But still I assume that many people familiar with Windows reckon *.* means all files – which it doesn't. – PerlDuck Oct 20 '18 at 14:06
  • @Zanna No, dot files are not included in glob expansion by default in most shells. In Bash, you can enable the dotglob option if you want this behavior. – tripleee Oct 22 '18 at 8:17
  • 1
    The reason what OP tries doesn't work can also be explained by saying that 1. wildcards are expanded before command execution starts, and if mv has more than two arguments, the last one must be a directory, and 2. Intuitively it might seem that mv will remember the expanded part of the filename, but of course it can't do that. By using a for loop, we get the shell to remember the name by storing it in a variable. Perl rename does the same thing internally iirc. Idk about rename.ui - OP doesn't show which they are using. – Zanna Oct 22 '18 at 11:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.