1

How can I create a Nautilus script that moves a the selected file into a new folder with the same name?

My starting point: /home/user/123 here 123 is a file with no extension

My goal here is to achieve this result: /home/user/123/123 here we have the same file 123 inside new folder also named 123

I can't figure this out, because every atempt I've made gave me the result: mkdir: cannot create directory `123': File exists

2 Answers2

1

You can't have two files by the same name at the same time, so you'll need to first create the directory under a temporary name, then move the file into it, then rename the directory. Or alternatively rename the file to a temporary name, create the directory, and finally move the file.

I see that Nautilus scripts can be written in any language. You can do this with the most pervasive scripting language, /bin/sh.

#!/bin/sh
set -e
for file do
  case "$file" in
    */*) TMPDIR="${file%/*}"; file="${file##*/}";;
    *) TMPDIR=".";;
  esac
  temp="$(mktemp -d)"
  mv -- "$file" "$temp"
  mv -- "$temp" "$TMPDIR/$file"
done

Explanations:

  • set -e aborts the script on error.
  • The for loop iterates over the arguments of the script.
  • The case block sets TMPDIR to the directory containing the file. It works whether the argument contains a base name or a file path with a directory part.
  • mktemp -d creates a directory with a random name in $TMPDIR.
  • First I move the file to the temporary directory, then I rename the directory. This way, if the operation is interrupted in the middle, the file still has its desired name (whereas in the rename-file-to-temp approach there's a point in time when the file has the wrong name).

If you want to remove the file's extension from the directory, change the last mv call to

mv -- "$temp" "$TMPDIR/${file%.*}"

${file%.*} takes the value of file and removes the suffix that matches .*. If the file has no extension, the name is left unchanged.

  • Thank you! I've tried and it works very well! I know this wasn't in my question: but could you please change the script so it removes the extension from the name in case the file happens to have an extension? Example: in case file is "123.txt" or 123.pdf or whatever, the folder gets named only "123" but the file inside it continues with it's original name "123.txt", etc. Could you do this, please? – oak-island-pirate May 27 '16 at 18:34
0

As you can see explained in here,

You cannot have a file and folder with the same name because directories are just a special kind of file

What I suggest is to rename > create > move with command validation && between them

e.g.

mv 123 123_tmp && mkdir 123 && mv 123_tmp 123/

You can use absolute paths

tachomi
  • 7,592
  • @ tachomi Yes, I know I can't have a file and a folder with the same name. But I was hoping I could achieve my goal as I intend to move the file inside the folder. I understand that at the moment the folder is created they will have the same name, because I guess I cannot move the file to a "not-yet-existing-folder". – oak-island-pirate May 27 '16 at 08:38