Tag Info

Hot answers tagged

63

Try this (works with csh, tcsh, ksh, zsh, bash, fish, mksh or pdksh): mv ~/folder/subfolder/file.{txt,sh}


45

You can't rename a file to . or .. because all directories already contain entries for those two names. (Those entries point to directories, and you can't rename a file to a directory.) mv detects the case where the destination is an existing directory, and interprets it as a request to move the file into that directory (using its current name). ...


44

Use brace expansion: mv very/long/path/to/filename.{old,new} would expand to mv very/long/path/to/filename.old very/long/path/to/filename.new


32

rsync would probably be a better option here. It's as simple as rsync -a subdir/ ./. My test tree in filename:contents format: ./file1:root ./file2:root ./dir/file3:dir ./dir/file4:dir ./subdir/dir/file3:subdir ./subdir/file1:subdir Running rsync: $ rsync -a -v subdir/ ./ sending incremental file list ./ file1 dir/ dir/file3 Gives: ./file1:subdir ...


28

You will have to copy them to the destination and then delete the source, using the commands cp -r * .. followed by rm -rf *. I don't think you can "merge" directories using mv.


27

A directory is (conceptually) a special "file" which contains a list of names, and the inode numbers those names point to. Some of names can be subdirectories. There is a special entry .. which points to the parent directory. So, its clear, changing the name of a file is easy: you just change the name in the directory entry, nothing else. This holds whether ...


27

First I'll make a test base - 5 files and one folder: touch file1 file2 file3 file4 file5 mkdir folder Next I'll run a test command. The -v option specifies that I want every command the shell executes to be printed to stderr. The -x option specifies that I want the same printed to stderr - but I want it done after the command is evaluated but before the ...


25

If you are using bash: for f in *.png; do mv "$f" "${f#image}"; done


25

If the last argument was a directory, you just moved all of the files and directories in your current working directory (except those whose names begin with dots) into that directory. If there were two files, the first file may have overwritten the second file. Here are some demonstrations: More than two files and the last argument is a file $ mkdir d1 d2 ...


23

With the folder called 'myfolder' and up one level in the file hierarchy (the point you want it to put) the command would be: mv myfolder/* . So for example if the data was in /home/myuser/myfolder then from /home/myuser/ run the command.


22

This should work: mkdir pretty_name && tar xf ugly_name.tar -C pretty_name --strip-components 1 -C changes to the specified directory before unpacking (or packing). --strip-components removes the specified number of directories from the filenames stored in the archive. Note that this is not really portable. GNU tar and at least some of the BSD ...


21

Use rsync(1): rsync \ --remove-source-files \ --chown=unicorn:unicorn \ /home/poney/folderfulloffiles /home/unicorn/


20

If you have rsync (remove --dry-run to do it for real): rsync --dry-run --remove-source-files -avHAX /unencrypted/ /encrypted Otherwise, using bash4+ and GNU stat: #!/bin/bash set -e shopt -s nullglob globstar for from in /unencrypted/**/*; do to="${from/\/un//}" if [[ -d "$from" ]]; then echo mkdir -p "$to" echo chmod "$(stat ...


18

You can use the advanced globbing patterns in some shells to match all the files in a directory except for those matching a particular pattern. For example, in ksh, bash or zsh, the command shopt -s extglob ## needed in bash only setopt ksh_glob ## needed in zsh only mv /source/!(*.bak) /destination will move all files in /source to /destination ...


18

This has worked. Perl's rename: rename -v 's/image//' *.png


18

With bash, you could use brace expansion mv blob/a_long_directory_name/{c/x.x,evenmore/y.y}


17

Programs connect to files through a number maintained by the filesystem (called an inode on traditional unix filesystems), to which the name is just a reference (and possibly not a unique reference at that). So several things to be aware of: Moving a file using mv does not change that underling number unless you move it across filesystems (which is ...


17

If you need to rename files in subdirectories as well, then you can do find /search/path -depth -name '* *' \ -execdir bash -c 'mv "$1" "${1// /_}"' _ {} \; Thank to @glenn jackman for suggesting -depth option for find and to make me think.


17

In Unix, almost everything is a file. A directory is a special type of file that from the user's perspective can "contain" other files. The error Not a directory occurs because your existing file is not a directory, and since a directory is a type of file, and there cannot be two identically named files in one directory, the operation cannot be performed.


17

rsync --verbose --archive --dry-run /oldisk/a/ /newdisk/a/ The --dry-run (or -n) will do a dry run, showing you what it would do without actually doing anything. If it looks ok, run the rsync without the -n option. This will be a copy, not a move, which isn't quite what you're doing, but is safer. The --archive (or -a) ensures all the ownership and ...


16

.. is not special, it is just that it already exists. On Unix, Dos and MS-Windows every directory has a directory . it links back to itself, and a directory .. it links to its parent directory (or self if root directory). If .. and . are special it is only because you can not remove them (actually you can, you just remove the directory that contains ...


15

zmv The zsh shell has a powerful batch rename command called zmv. First you need to enable the zmv command as follows (this can go into your ~/.zshrc). autoload zmv The basic syntax is zmv PATTERN REPLACEMENT. The pattern is a shell glob expression. Parts of the pattern can be surrounded by parentheses. The replacement text can contain $1, $2, etc. to ...


15

I guess you see this � invalid character because the name contains a byte sequence that isn't valid UTF-8. File names on typical unix filesystems (including yours) are byte strings, and it's up to applications to decide on what encoding to use. Nowadays, there is a trend to use UTF-8, but it's not universal, especially in locales that could never live with ...


15

rsync can delete the source after copies with the --remove-source-files parameter. This should be a convenient way to do what you'd like. From the rsync man page: --remove-source-files sender removes synchronized files (non-dir)


15

mv cannot merge or overwrite directories, it will fail with the message "mv: cannot move 'a' to 'b': Directory not empty", even when you're using the --force option. You can however use rsync with the --remove-source-files option (and possibly others) to merge one directory into another. rsync won't delete any directories, so you will have to do something ...


14

First the shell expands ./* to all files in the current directory (except files starting with a dot). if there is only one file: mv fails if there are two file: the first one is moved to the second (which therefore get lost) if there are more than two files: if the last one is a directory: all files are moved into this directory otherwise mv fails.


14

You can also use rename (part of the util-linux package). rename .txt .sh ~/folder/subfolder/file.txt See the rename man page for more details.


13

Just found out here that jhead can do it all for you! :) jhead -autorot -nf%Y-%m-%d_%H-%M-%S *.jpg


13

Don't try to parse find output except as a last resort. It is important to realize that on Unix file systems, file names are not strings (a common misconception) but rather binary blobs which can contain any character except / and the null character. Parsing file names safely and correctly is enough of a pain that 99% of the time you'll just want to avoid ...



Only top voted, non community-wiki answers of a minimum length are eligible