TOPIC: CP
Something to try when you get a message like this caused by a filename with a leading hyphen: "mv: illegal option -- u"
3rd December 2024Recently, I downloaded some WEBP files from Ideogram and attempted to move them to another folder. That is when I got the message that you see in the title of this entry. Because I had not looked at the filenames, I baffled when I got this from a simple command that I had been using with some success until then. Because I was using an iMac, I tried the suggestion of installing coreutils
to get GNU mv
and cp
to see if that would help:
brew install coreutils
The above command gave me gmv
and gcp
for the GNU versions of mv
and cp
that comes with macOS. Trying gmv
only got me the following message:
gmv: cannot combine --backup with --exchange, -n, or --update=none-fail
The ls
command could list all files, but not the WEBP ones. Thus, I executed the following to show what I wanted:
ls | grep -i webp
That got around the problem by doing a subset of the directory listing. It was then that I spotted the leading hyphen. To avoid the problem tripping me up again, I renamed the offending file using this command:
mv -- -iunS9U4RFevWpaju6ArIQ.webp iunS9U4RFevWpaju6ArIQ.webp
Here, the --
switch tells the mv
command not to look for any more options and only to expect filenames. When I tried enclosing the filename in quotes, I still got problems, even that might have because I was using double quotes instead of single quotes. Another option is to escape the leading hyphen like this:
mv ./-iunS9U4RFevWpaju6ArIQ.webp iunS9U4RFevWpaju6ArIQ.webp
Once the offending file was renamed, I could move the files to their final location. That could have used the --
option too, saving me an extra command, only for my wanting this not to trip me up again. Naturally, working in Finder might have avoided all this as much as not having a file with a leading hyphen in its name, but there would have been nothing to learn then.
Avoiding permissions, times or ownership failure messages when using rsync
22nd April 2023The rsync
command is one that I use heavily for doing backups and web publishing. The latter means that it is part of how I update websites built using Hugo because new and/or updated files need uploading. The command also sees usage when uploading files onto other websites as well. During one of these operations, and I am unsure now as to which type is relevant, I encountered errors about being unable to set permissions.
The cause was the encompassing -a
option. This is a shorthand for -rltpgoD
, and the individual options perform the following:
-r
: recursive transfer, copying all contents within a directory hierarchy
-l
: symbolic links copied as symbolic links
-t
: preserve times
-p
: preserve permissions
-g
: preserve groups
-o
: preserve owners
-D
: preserve device and special files
The solution is to some of the options if they are inappropriate. The minimum is to omit the option for permissions preservation, but others may not apply between different servers either, especially when operating systems differ. Removing the options for preserving permissions, groups and owners results in something like this:
rsync -rltD [rest of command]
While it can be good to have a more powerful command with the setting of a single option, it can mean trying to do too much. Another way to avoid permissions and similar errors is to have consistency between source and destination files systems, but that is not always possible.
Copying only updated new or updated files by command line in Linux or Windows
2nd August 2014With a growing collection of photographic images, I often find myself making backups of files using copy commands and the data volumes are such that I don't want to keep copying the same files over and over again, so incremental file transfers are what I need. So commands like the following often get issued from a Linux command line:
cp -pruv [source] [destination]
Because this is on Linux, it is the bash shell that I use, so the switches may not apply with others like ssh, fish or ksh. For my case, p
preserves file properties such as its time and date and the cp
command does not do this always, so it needs adding. The r
switch is useful because the copy then in recursive, so only a directory needs to be specified as the source and the destination needs to be one level up from a folder with the same name there to avoid file duplication. It is the u
switch that makes the file copy incremental, and the v
one issues messages to the shell that show how the copying is going. Seeing a file name issued by the latter does tell you how much more needs to be copied and that the files are going where they should.
What inspired this post though is my need to do the same in a Windows session, and issuing xcopy
commands will achieve the same end. Here are two that will do the needful:
xcopy [source] [destination] /d /s
xcopy [source] [destination] /d /e
In both cases, it is the d
switch that ensures that the copy is incremental, and you can add a date too, with a colon between it and the /d
, if you see fit. The s
switch copies only directories that contain files, while the e
one copies even empty directories. Using the d
switch without either of those did not trigger any copying action when I tried, so I reckon that you cannot do without either of them. By default, both of these commands issue output to the command line so you can keep an eye on what is happening, and this especially is useful when ensuring that files are going to the right destination because the behaviour differs from that of the bash shell on Linux.
Renaming multiple files in Linux
19th August 2012The Linux and UNIX command mv
has a number of limitations, such as not overwriting destination files and not renaming multiple files using wildcards. The only solution to the first that I can find is one that involves combining the cp
and rm
commands. For the second, there's another command: rename. Here's an example like what I used recently:
rename s/fedora/fedora2/ fedora.*
The first argument in the above command is a regular expression much like what Perl is famous for implementing; in fact, it is Perl-compatible ones (PCRE) that are used. The s before the first slash stands for substitute, with fedora
being the string that needs to be replaced and fedora2
being what replaces it. The third command is the file name glob that you want to use, fedora.* in this case. Therefore, all files in a directory named fedora
will be renamed fedora2
regardless of the file type. The same sort of operation can be performed for all files with the same extension when it needs to be changed, htm
to html
, for instance. Of course, there are other uses, but these are handy ones to know.
Copying only updated and new files
20th October 2008With Linux/UNIX, the command line remains ever useful and allows you to do all manner of things, including file copying that only adds new files to a destination. Here's a command that accomplishes this on Linux:
cp -urv [source] [destination]
The u
switch does the update while r
ensures recursion (by default, cp
only copies files from a source directory and not anything sitting in subfolders) and v
tells the command to tell the user what is happening.
Though buried and hardly promoted, Windows also has its command line and here's what accomplishes a similar result:
xcopy /d /u [source] [destination]
Anything's better than having to approve or reject every instance where source and destination files are the same or, even worse, to overwrite a file when it is not wanted.