Rename filename to filename_inode (Unix) - unix

I would like to move my file from one location to another.
In the process, I want my filename to change from filename to filename_inode
Any idea how I can do that?
I know I can get the inode using
ls -i $filename
I would prefer a solution that does not require me to download any addition features.

In Bash, you can do the following
FILE=*<your-file-name>*
mv $FILE ${FILE}_$(ls -li $FILE | awk '{ print $1}')

Related

Removing an Extension in a filename in HDFS using shell script

Please help me how to remove an extension in a filename in HDFS using
unix shell script.
For example, my initial filename is sample.txt.gz. I want to remove
the .gz in the filename.
Here what I have done so far.
#Parameters
baseDirHdfs=${1} dss=${2} ds=${3} processDirHdfs=${4} filename=${5} kerberosKeytab=${6} kerberosPrincipal=${7}
kinit -kt ${kerberosKeytab} ${kerberosPrincipal}
#Removing .gz extension
newFilename=echo ${filename} | cut -f1-6 -d '.'
#Decompressing .GZ Files
hdfs dfs -cat /${baseDirHdfs}/${dss}/${ds}/${processDirHdfs}/${filename}|gzip -d|hdfs dfs -put - /${baseDirHdfs}/${dss}/${ds}/${processDirHdfs}/${newFilename}
Normally, the filename and extensions are separated by a dot(.)
So, something like this will do it:
mayankp#mayank: $file=myfile.sh
mayankp#mayank: $file_name=`echo $file| awk -F'.' '{print $1}'`
mayankp#mayank: $echo $file_name
myfile
Let me know if this helps.

Rename files based on pattern in path

I have thousands of files named "DOCUMENT.PDF" and I want to rename them based on a numeric identifier in the path. Unfortunately, I don't seem to have access to the rename command.
Three examples:
/000/000/002/605/950/ÐÐ-02605950-00001/DOCUMENT.PDF
/000/000/002/591/945/ÐÐ-02591945-00002/DOCUMENT.PDF
/000/000/002/573/780/ÐÐ-02573780-00002/DOCUMENT.PDF
To be renamed as, without changing their parent directory:
2605950.pdf
2591945.pdf
2573780.pdf
Use a for loop, and then use the mv command
for file in *
do
num=$(awk -F "/" '{print $(NF-1)}' file.txt | cut -d "-" -f2);
mv "$file" "$num.pdf"
done
You could do this with globstar in Bash 4.0+:
cd _your_base_dir_
shopt -s globstar
for file in **/DOCUMENT.PDF; do # loop picks only DOCUMENT.PDF files
# here, we assume that the serial number is extracted from the 7th component in the directory path - change it according to your need
# and we don't strip out the leading zero in the serial number
new_name=$(dirname "$file")/$(cut -f7 -d/ <<< "$file" | cut -f2 -d-).pdf
echo "Renaming $file to $new_name"
# mv "$file" "$new_name" # uncomment after verifying
done
See this related post that talks about a similar problem: How to recursively traverse a directory tree and find only files?

Download and change filename to a list of urls in a txt file

Let's say I have a .txt file where I have a list of image links that I want to download.
exaple:
image.jpg
image2.jpg
image3.jpg
I use: cat images.txt | xargs wget and it works just fine
What I want to do now is to provide another .txt file with the following format:
some_id1:image.jpg
some_id2:image2.jpg
some_id3:image3.jpg
What I want to do is to split each line in the ':' , download the link to the right, and change the downloaded file-name with the id provided to the left.
I want to somehow use wget image.jpg -O some_id1.jpg
So the output will be:
some_id1.jpg
some_id2.jpg
some_id3.jpg
Any ideas ?
My goto for such tasks is awk:
while read line; do lfn=`echo "$line" | awk -F":" '{ print $1".jpg" }'` ; rfn=`echo "$line" | awk -F":" '{ print $2 }'` ; wget $rfn -O $lfn ; done < images.txt
This presumes, of course, all the local file names should have the .jpg extension.

How can I rename a file on unix using characters in filename?

I have a bunch of files in a unix directory that look like the following:
filename_1234567.txt
I need to rename them by copying the last three characters of each filename
to the front of the filename like this:
567_filename_1234567.txt
Note: Both the filename and extension are variable.
I'm running this on a Solaris box.
Thanks in advance!
One possibility:
\ls *.txt | sed 's/\(.*\)\(...\).txt/mv \1\2.txt \2_\1.txt/' | sh
(probably wise to write that with echo mv, while you're double-checking it does what you think it does).
I can't decide if the alternative
sed 's/\(\(.*\)\(...\).txt\)/mv \1 \3_\2.txt/'
is more robust, or just way too fussy.
#for file in *_*.*
for file in `ls *_*.txt`
do
last_3="$(echo $file | grep -o "...\...."|cut -d'.' -f1)"
cp $file ${last_3}_${file}
done

batch rename to change only single character

How to rename all the files in one directory to new name using the command mv. Directory have 1000s of files and requirement is to change the last character of each file name to some specific char. Example: files are
abc.txt
asdf.txt
zxc.txt
...
ab_.txt
asd.txt
it should change to
ab_.txt
asd_.txt
zx_.txt
...
ab_.txt
as_.txt
You have to watch out for name collisions but this should work okay:
for i in *.txt ; do
j=$(echo "$i" | sed 's/..txt$/_.txt/')
echo mv \"$i\" \"$j\"
#mv "$i" "$j"
done
after you uncomment the mv (I left it commented so you could see what it does safely). The quotes are for handling files with spaces (evil, vile things in my opinion :-).
If all files end in ".txt", you can use mmv (Multiple Move) for that:
mmv "*[a-z].txt" "#1_.txt"
Plus: mmv will tell you when this generates a collision (in your example: abc.txt becomes ab_.txt which already exists) before any file is renamed.
Note that you must quote the file names, else the shell will expand the list before mmv sees it (but mmv will usually catch this mistake, too).
If your files all have a .txt suffix, I suggest the following script:
for i in *.txt
do
r=`basename $i .txt | sed 's/.$//'`
mv $i ${r}_.txt
done
Is it a definite requirement that you use the mv command?
The perl rename utility was written for this sort of thing. It's standard for debian-based linux distributions, but according to this page it can be added really easily to any other.
If it's already there (or if you install it) you can do:
rename -v 's/.\.txt$/_\.txt/' *.txt
The page included above has some basic info on regex and things if it's needed.
Find should be more efficient than for file in *.txt, which expands all of your 1000 files into a long list of command line parameters. Example (updated to use bash replacement approach):
find . \( -type d ! -name . -prune \) -o \( -name "*.txt" \) | while read file
do
mv $file ${file%%?.txt}_.txt
done
I'm not sure if this will work with thousands of files, but in bash:
for i in *.txt; do
j=`echo $i |sed 's/.\.txt/_.txt/'`
mv $i $j
done
You can use bash's ${parameter%%word} operator thusly:
for FILE in *.txt; do
mv $FILE ${FILE%%?.txt}_.txt
done

Resources