1

I have a directory which has several files and directories in it. I want to add extension .ext to all files. Some files have extension(different types of extensions) and some files don't have extension.

I am using Ububtu 16.04 .

I read several answers regarding to it but I was not able to understand them.

A Q
  • 166
  • 2
  • 13
  • With the command mv Like in this question: https://stackoverflow.com/questions/6114004/add-file-extension-to-files-with-bash – TheKrogrammer Oct 05 '17 at 05:36
  • It is not working. – A Q Oct 05 '17 at 05:43
  • 1
    "It is not working" Is NOT helpful at all.... WHAT "is not working"? Does it do stupid things? Does it throw errors on you? how did you tried it? Show code... – Mischa Oct 05 '17 at 05:47
  • When I enter the command `mv "${file}" "${file}".jpg` terminal shows `mv cannot stat ' ' : No such file or directory` – A Q Oct 05 '17 at 06:02

2 Answers2

1

In bash:

$ for f in * ; do if [ -f "$f" ] ; then t="${f%.*}" ; mv -i "$f" "$t".ext ; fi ; done

Explained:

for f in *               # loop all items in the current dir
do 
  if [ -f "$f" ]         # test that $f is a file
  then
    t="${f%.*}"          # strip extension off ie. everything after last .
    mv -i "$f" "$t".ext  # rename file with the new extension
  fi
done

Test:

$ touch foo bar baz.baz 
$ mkdir dir ; ls -F
bar  baz.baz  dir/  foo
$ for f in * ; do if [ -f "$f" ] ; then t="${f%.*}" ; mv -i "$f" "$t".ext ; fi ; done
$ ls
bar.ext  baz.ext  dir/  foo.ext

Some overwriting might happen if, for example; there are files foo and foo.foo. Therefore I added the -i switch to the mv. Remove it once you understand what the above script does.

James Brown
  • 36,089
  • 7
  • 43
  • 59
  • It is adding extension to a directory(only problem left) also that is I don't want to do. It is adding extension correctly to files having extension previously.I missed directory in the question earlier and now have edited it. Sorry for that. – A Q Oct 05 '17 at 05:54
  • Is '%' being used to strip off everything after '.' . – A Q Oct 05 '17 at 06:08
  • `%.*` means striping off last period and everything after that. That might cause a problem also if you have "extensionless" file with a period in the name `like.this` vs. `or.this.txt`. – James Brown Oct 05 '17 at 06:14
  • Added a test to process only files. – James Brown Oct 05 '17 at 06:18
0

Is this what you require:

find . -name "*" -type f|awk 'BEGIN{FS="/"}{print $2}'|awk 'BEGIN{FS=".";ext=".png"}{system("mv "$0" ./"$1ext"")}'

[root@myIP check]# ls -lrt
total 0
-rw-r--r--. 1 root root 0 Oct  5 08:08 abc
-rw-r--r--. 1 root root 0 Oct  5 08:08 hello.txt
-rw-r--r--. 1 root root 0 Oct  5 08:08 something.gz
[root@myIP check]# find . -name "*" -type f|awk 'BEGIN{FS="/"}{print $2}'|awk 'BEGIN{FS=".";ext=".png"}{system("mv "$0" ./"$1ext"")}'
[root@myIP check]# ls -lrt
total 0
-rw-r--r--. 1 root root 0 Oct  5 08:08 abc.png
-rw-r--r--. 1 root root 0 Oct  5 08:08 hello.png
-rw-r--r--. 1 root root 0 Oct  5 08:08 something.png
abhishek phukan
  • 751
  • 1
  • 5
  • 16