LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-05-2022, 10:08 AM   #1
hirom
LQ Newbie
 
Registered: Jun 2022
Posts: 2

Rep: Reputation: 0
How to rename the files to extracted 7 last letters from original name


Hello, experts,
I need to rename a lot of files to extracted 7 last letters from original name;
ex. from "zyxhheddbaaa00106.jpg" to "106.jpg"
As the original filename text length varies, it seems that I need to use both "rev" and "cut" command.

To begin with "rev" command, I thought the command below work,
Code:
for f in `find . -name *.*`; do g="$(rev $(basename $f))" | mv $f "$(dirname $f)/$(basename $g)" ; done
However, I got the error message below.

basename: missing operand
Try 'basename --help' for more information.
rev: cannot open zzzhheddbaaa0381.jpg: No such file or directory
mv: './066/zzzhheddbaaa0381.jpg' and './066/zzzhheddbaaa0381.jpg' are the same file

My questions are:

Regarding "basename": I don't understand this error as in the next line it seems that the file name is read correctly.
Regarding "rev": How to reverse the text itself rather than reading a file?

I appreciate your advice, thank you in advance.
 
Old 06-05-2022, 10:19 AM   #2
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,730

Rep: Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743
First, you are asking two questions: one about extracting the last seven characters from a string and another about renaming files. These can easily be combined into one process, but ARE different questions.

Second, are you doing this in BASH? This may be important in that if a shell with string handling and substring features is involved then no external utilities should be required for extracting the last seven characters form a string.

On the renaming a file, I would add some handling in the case that two file names might happen to have the same last seven characters, even if all others differ.

Finally a question of my own, before we discuss code: is this homework? If it is, what have you done to address it so far? Can we see your work?
 
1 members found this post helpful.
Old 06-05-2022, 10:37 AM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,150
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Example:
Code:
a=(
zyxhheddbaaa00106.jpg
hheddbaaa00106.jpg
eddbaaa00106.jpg
dbaaa00106.jpg
baaa00106.jpg
aaa00106.jpg
a00106.jpg
0106.jpg
)

for i in "${a[@]}"; do
    echo ""$i" ----> "${i[@]: -7}""
done
 
Old 06-05-2022, 10:51 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,343
Blog Entries: 3

Rep: Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754
If you use rename then you can use full perl regular expressions to manipulate the name.
 
Old 06-05-2022, 10:59 AM   #5
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,150
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Quote:
for f in `find . -name *.*`
For f in current dir find everything.

Code:
for f in *; do
    echo "mv "$f" --> "${f[@]: -7}""
    sleep 1
done
 
Old 06-05-2022, 11:51 AM   #6
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,621

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555
Quote:
Originally Posted by hirom View Post
I need to rename a lot of files to extracted 7 last letters from original name;
ex. from "zyxhheddbaaa00106.jpg" to "106.jpg"
WHY?

How certain are you that it's always seven characters and the result is always unique and correct?

Have you confirmed that you'll never have "whatever01106.jpg" or "whatever00106.jpeg"?

Are you aware what might happen if such a script is run on a server and is inadvertently used against a file named ".htaccess" ?

 
1 members found this post helpful.
Old 06-05-2022, 01:56 PM   #7
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,150
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Quote:
Are you aware what might happen if such a script is run on a server and is inadvertently used against a file named ".htaccess" ?
That's a good thought.

Let me see.

This doesn't get hidden files
Code:
for i in *; do
    if [[ "$i" =~ ^\. ]]; then
        echo "Hidden file --> "$i""
    else 
        echo "Not hidden --> "$i""
    fi
done
This does
Code:
for i in .*; do
    if [[ "$i" =~ ^\. ]]; then
        echo "Hidden file --> "$i""
    else 
        echo "Not hidden --> "$i""
    fi
done
These will get everything

Code:
shopt -s dotglob
for i in *; do
    if [[ "$i" =~ ^\. ]]; then
        echo "Hidden file --> "$i""
    else 
        echo "Not hidden --> "$i""
    fi
done

for i in .[!.]* *; do
    if [[ "$i" =~ ^\. ]]; then
        echo "Hidden file --> "$i""
    else 
        echo "Not hidden --> "$i""
    fi
done
 
1 members found this post helpful.
Old 06-07-2022, 11:55 AM   #8
hirom
LQ Newbie
 
Registered: Jun 2022
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you all for your comments and thoughtful discussions.
I have only limited access time to the Linux machine, so I will try the commands tomorrow.
The data is jpg files of a continuous subjective experiment stored in different folders /"experiment_No"/"Subject's_name_etc"-"3_digits_number".jpg .
There are images with same 3 digits number in different folders, but thankfully, they are not in the same folder.
The reason why I need to delete the letters is to delete the subjects' name to make it anonymous before passing the data to an external collaborator.
However, I learned from you that it is important to make commands as safe as possible.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] tar extracted paths not retaining original ownership genogebot Linux - Software 11 10-22-2016 10:55 PM
to grep 100 letters out of 500 letters on certain criteria l_ravi69 Programming 3 10-13-2013 11:37 AM
Can't stop fast typing double letters from "skipping" one of the letters. Lola Kews Ubuntu 3 04-20-2013 03:21 PM
Shell script to get name of file, delete original file, rename blank file chrisgti Linux - General 11 09-15-2012 02:49 AM
include file name to extracted files miss_dodi Linux - Newbie 13 04-29-2011 04:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration