I would like to preserve the movie name before renaming them. Can this be done for all files in a folder, at ones?

So from current movie name "A Clockwork Orange_(1971) Stanley Kubrick(DivX).avi", script will name a text file for "A Clockwork Orange_(1971) Stanley Kubrick(DivX)" and put the full name; "A Clockwork Orange_(1971) Stanley Kubrick(DivX).avi", in the new file...

Please explain step by step.

  • You mean you want to create a text file with the same file name as a movie file, and put the movie's file name as content in the text file? I don't think I fully understand what you want to achieve...
    – Byte Commander
    May 15, 2017 at 20:31
  • 1
    Something like for f in *.avi; do echo "$f" > "${f%.avi}.txt"; done you mean? or do you want all the names in a single file? May 15, 2017 at 20:31
  • Looks good to me, one file per movie. Could I run the above in a terminal? just cd to the folder, and sudo the above line? If I have other file extension; should I just change to .divx or ie .mp4? Thanks for your help! May 16, 2017 at 18:55
  • I get an syntax error for "do" May 16, 2017 at 20:53
  • @TerjeFolkvord Don't use sudo with steeldriver's command, because 1) the command doesn't require root, and 2) sudo can only run files; that's why you got a syntax error.
    – wjandrea
    May 17, 2017 at 17:13

1 Answer 1


Steeldriver done beat me to it, and more elegantly. I'm not gonna be in his class for a decade at least. But since I already wrote and tested this, I'll post it, FWIW. The following code is to be put in a script. Then you run the script in a terminal with the sole argument being the directory containing the files you want to deal with. If you wanted to run it from a gui file browser, you'd need to invoke it with something like xterm -e /path/to/this_script.sh $f, f being whatever variable the file browser passes on the /path/to/directory as.

So open a plain text file with an editor, something like geany, leafpad, or (shudder) gedit.

Copy the code below into it, starting with the "#!/bin/bash two lines below this.

##### Do NOT copy this line, start with the next line beginning "#!"


# repeats until you enter a valid directory
# you might add a test to make sure you have adequate permissions.
while [ ! -d "$DIr" ] || [ $DIr = / ]; do
      read -p "$DIr is not a valid argument. Enter a 
      directory other than '/' with files you want to process:" DIr

# canonicalizes directory name
DIr="$(readlink -f "$DIr")"

for FILE in $DIr/*; do
    BASENAMe="$(basename "$FILE")"
    BASE_NAME_minus_ext="$(echo -n "$BASENAMe" | rev | cut -d'.' -f2- | rev)"

    # If you want 'em all in a single list, instead use:
    # echo "$BASENAMe" >> "$DIr/List_of_names_minus_extensions"
    echo "$BASENAMe" > "$DIr"/"$BASE_NAME_minus_ext"


##### Do NOT copy this line. Copy through and including "done" just above. No more.

Save the file with a convenient name, like "list.sh", without the quote marks. Move it to one of the directories in your path. You can display the path with the command:

echo $PATH

That will return a string which is a bunch of directories seperated by colons ( the ":" character). The individual directory names all begin with a "/". They do NOT include the ":". Any of those directories will do. If it is in your path, I suppose the most conventional one would be:


but it really doesn't matter as long as it is one of them. If you did that with a graphical file browser (like Nautilus, Thunar, or Pcmamfm), right click on the script (i.e. the file you just created and moved) and click "properties" and then "permissions" if that is a separate tab. Set it as executable by you. All sorts of ways to do that. Most convenient is to set as owned by your user name and the group of the same name and check executable, readable and writable by owner and group but none of those by other.

If you are doing this from command line:

chmod -v ug+r+w+x /usr/local/bin/list.sh 

using whichever of the directories on the path you put the file in and whatever filename you gave it.

Or, if that fails, do these 2, one at a time, in order:

sudo chown your_user_name:your_user_name /usr/local/bin/list.sh
sudo chmod -v ug+r+w+x /usr/local/bin/list.sh 

Now you should have an executable script that can be invoked by typing it's name into a terminal and pressing enter, either plain like this:


or with the name of the directory you want it to act on like this:

list.sh /path/to/directory_that_has_your_movies

If you don't include the full /path/to/directory_you_want_acted_upon, it will make a reasonable guess about where to find it. It might guess wrong. If you want to save yourself keystrokes you can give the script a shorter name like "lis" or if you want to make it easier to remember what it does, you can give it a longer name like "list_files_within_without_extensions.sh. The ".sh" ending is a common convention for script names just so you will know what the file is by looking at its name. you can name it anything, even "George Humperdink, III" if you want to, but spaces and commas tend to cause trouble.

Some more thoughts: In light of your response to sudodus, I must caution, do NOT use sudo to run either his command or my script. I can't think of any good reason for movies to be owned by root or in a directory owned by root. They absolutely should NOT be run in a viewer by root. We don't have much of a malware problem in the 'nix ecosystem, but invoking media players, even pic viewers, as root, is asking for trouble and if enough people ask for trouble, someone will provide it. Therefore the files MUST be readable by a plain user, and it would be darned awkward to keep them in a directory that wasn't readable, writable, and "executable" by a plain user. If either his command or my script fails in a way that sudo would have prevented, then you need to change the perms of the files or the directory they are in. Using sudo would be like setting fire to the furniture because the AC has made the room too cold. It will just give you MORE permission problems by creating root owned list files. Never use sudo unless you HAVE to, and then only if it makes sense. If it doesn't make sense to have to use sudo, then fix the problem, don't work around it.

Also, yes his command will work for other extensions if you make the logical changes in it. The script works for any extensions as is.

You'll note near the middle is a commented out line regarding whether you want this to make one list in the directory it acts on or make a separate text file for each movie. Putting a "#" mark at the beginning of a line is referred to as "commenting out" the line. It means it is a comment for humans to read and bash will ignore it. You can make the script work either way by changing which of the pair of lines is commented. One should be commented, and the other not commented.

  • What's with the funky variable names?
    – wjandrea
    May 15, 2017 at 22:51
  • De gustibus non est disputandum. They're valid. May 15, 2017 at 23:15
  • Yes, I've just never seen that form of case used before. If there a particular rationale behind it?
    – wjandrea
    May 15, 2017 at 23:17
  • Yes, some people complain about all upper case. In some RARE instances, it is a valid complaint. But I like my variables to jump out to visual inspection. It makes it easier to read the script for me. So when I think of it, I use camel case like that. I also try to use forms that are valid for bash, dash, and tcsh. Now, don't go dissin' tcsh. May 15, 2017 at 23:22
  • Hi Lew, I managed to get it to work on some test file, but only once in 5-6 runs... What I do; cd to the folder with files, enter sudo and paste in your code, with all entry above, get the question to enter a directory other than "/", and do so... how am I doing so far? May 16, 2017 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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