0

[UPDATE-1]: If possible, give me cons for motivation to learn the simplest commands.


So, I have one file in directory:

ls | grep *.mp4 

video.mp4

I use this command to randomly rename & move file in another directory:

for a in *.mp4; do mv -n "$a" "${a/*/$RANDOM.mp4}"; done;  mv *.mp4 /home/misdeed/Documents

Is ok, I get randomly name for video.mp4

cd Documents; ls | grep *.mp4

19889.mp4

Qestion:

How to use is:

a="This is name file"
echo $a

This is name file

for rename *.mp4 (not randomly) and for get

This is name file.mp4


I need something like it:

  • a="This is name file"
  • COMMAND FOR RENAME BUT WITHOUT $RANDOM. I WANT USE $a in renamed file
  • 1
    mv something.mp4 "$a".mp4? Is the problem you don't know in advance what something is? In that case what if there are two files matching *.mp4? Is the premise there's always exactly one such file? – Kamil Maciorowski May 28 at 17:36
  • @KamilMaciorowski only one file video.mp4: How I get it: ttyrec -> ttyrec2gif -> ffmpeg -i tty.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.mp4 – 0-Level UNIX Monk May 28 at 17:50
  • @KamilMaciorowski, upps, I try you solve! It helps: mv *.mp4 "$a".mp4; mv *.mp4 /home/misdeed/Documents; cd Documents; ls | grep *.mp4. Thank you very much! – 0-Level UNIX Monk May 28 at 17:58
  • So the name is always video.mp4, right? Then mv video.mp4 "$a".mp4. I'm confused and maybe not getting this right. You started with relatively complex code that involves random names and iterating over files, but in the end the question is "how to rename a file with a known name to a new name which is stored in a variable?" So basic mv and shell variable usage. If the variable is available when ffmpeg runs, you can pass "$a".mp4 instead of video.mp4 in the first place. – Kamil Maciorowski May 28 at 18:09
  • And please drop this *.mp4! It only works because you have just one matching file. Frankly ls | grep *.mp4 is very wrong but it only affects what you see. If you ask a new question "what is wrong with ls | grep *.mp4?" you will learn a lot. – Kamil Maciorowski May 28 at 18:11
1

Let's say the original name is known in advance and it's video.mp4. You rename it with

mv video.mp4 "$a".mp4

Notes:

  • If the variable is empty (or unset), the final name will be .mp4. Names starting with dot (.) are "hidden", plain ls skips them, you need ls -a to see them.
  • If there is already a file with the target name, it will be lost and the former video.mp4 file will take its name. To prevent mishaps consider mv -i; it will prompt for confirmation if the destination path exists.
0

I assume you want the new random file name printed? In that case, the easiest is to reuse the value of your random variable.

for file in *.mp4; do
  RANDOM_NAME="${file/*/$RANDOM.mp4}";
  mv -n "$file" /home/misdeed/Documents/"$RANDOM_NAME";
  echo "This is name $RANDOM_NAME.mp4";
done
  • No, no. I want use assigned value of bash var for rename file. Ok? – 0-Level UNIX Monk May 28 at 16:22

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.