-2

I have files name filename.gz.1 and i need to rename them to filename.gz, There are allot of files and every name is different,

I know i cant to this for i in $(ls); do mv $i $i.1; done, But can i do this reverse, from filename.gz.1 to filename.gz and keep the original file name?

I have tried this,

for i in $(ls | grep "xz\|gz"); do echo "mv $i $i | $(rev) | $(cut -c3-) | $(rev) |)"; done

But this ignores my Pipes.

Can anyone help me?

  • 1
    Did you see [Linux: remove file extensions for multiple files](https://stackoverflow.com/q/4509485/3266847)? – Benjamin W. Oct 12 '21 at 12:24
  • 3
    Btw.: [Why *not* parse `ls`?](http://unix.stackexchange.com/questions/128985/why-not-parse-ls) – Cyrus Oct 12 '21 at 12:30
  • If all files have extra ".1" you can just cut this one. If you have ".2" ".11" it will require a bit more conditions – Saboteur Oct 12 '21 at 12:33

3 Answers3

0

You can use

for i in ...;
do
    echo "$i --> ${i%.*}"
done

The ${i%.*} will remove at the end (%) what match a period followed by anything (.*)

Mathieu
  • 8,840
  • 7
  • 32
  • 45
0

You have to use --extended-regexp when using a pipe as separation.

echo -e "foo.xz\nbar.gz\nbaz" | grep -E "(xz|gz)"

and don't use ls for list your files, it is not save. instead you can use a loop to search for files.

for file in /PATH/TO/FILES; do
echo $file
done

I would also recommend to use regex to search for filenames ending with a number, like ...

if [[ $file =~ [[:digit:]]$ ]]; then
  echo ${file%.*}
fi

Mario
  • 679
  • 6
  • 10
-1

I was able to resolve it like this:

for i in $(ls | grep "xz\|gz"); do echo "mv $i $i" | rev | cut -c3- | rev; done | bash