SlackwareThis Forum is for the discussion of Slackware Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi: I have a set of files under the same directory, all filenames beginning with the same prefix. How could I take away the prefix from all the filenames?
All I need is a way to do the operation for only one file, as then I could write a script to do the same within a loop.
Hi: I have a set of files under the same directory, all filenames beginning with the same prefix. How could I take away the prefix from all the filenames?
All I need is a way to do the operation for only one file, as then I could write a script to do the same within a loop.
If you use the Xfce desktop, there's a nifty graphical application for just what you are trying to do: Thunar Bulk Rename. I use it all the time for mass-renaming music files for my car stereo's USB stick, which can't handle subdirectories.
If in the future you might need to rename files again, you might also want to check out rename: a perl script by ex-Slackware user Aristotle Pagaltzis. There's also a tutorial.
If in the future you might need to rename files again, you might also want to check out rename: a perl script by ex-Slackware user Aristotle Pagaltzis. There's also a tutorial.
Although you might want to rename it first to avoid conflict with the stock rename in /usr/bin
Thanks for suggesting 'rename'. But could somebody tell me what's the problem with this script?:
Code:
semoi@server:~/STORE1/Nonsoft/libros/chess/nuevo$ cat ren.sh
#!/bin/bash
# PROG APPL: Rename a set of files.
# El script es poco flexible. No tiene argumentos de entrada. It's hard
# coded. In current dir there are
# Averbakh,\ Yuri\ -\ Comprehensive\ 1.djvu
# Averbakh,\ Yuri\ -\ Comprehensive\ 2.djvu
# Averbakh,\ Yuri\ -\ Comprehensive\ 3.djvu
# The script renames them to
# BOOK01
# BOOK02
# BOOK03
# s is sed's substitute command, like s in vim.
LIST="$(ls Aver*)" #ls interpretado como orden por los parentesis
for i in "$LIST" ; do
INPUT="$i"
OUTPUT=$(echo "$INPUT" | tr ' ' '_')
# OUTPUT=$(echo "$OUTPUT" | sed s/Averbakh,_Yuri_-_Comprehensive_//)
# mv "$INPUT" "$OUTPUT"
echo -n "Output= "
echo "$OUTPUT"
done
# The second command could be tr as well, or both commands sed. Either
# tr or sed can be used in both lines.
#Averbakh,\ Yuri\ -\ Comprehensive\ Chess\ Endings\ 1\ -\ Bishop\ &\ Knight\ Endings.pdf*
semoi@server:~/STORE1/Nonsoft/libros/chess/nuevo$
It should replace spaces with underscores ('_'). OUTPUT should contain a single filename. However, the loop is traversed only once, which makes me think OUTPUT contains all of the filenames concatenated:
Writing it in the condensed form solves the problem. I mean, now the loop is traveled as many times as there are files in the directory. But now I want to know what the trouble is with
Code:
LIST="$(ls Aver*)"
for i in "$LIST" ; do
Why is the loop traveled only once? If I write $LIST without quotes then the spaces in the filenames make the loop to be traveled once for every word in the filename.
Why is the loop traveled only once? If I write $LIST without quotes then the spaces in the filenames make the loop to be traveled once for every word in the filename.
If you use quotes the whole list is taken as a single word! To avoid problems with blank spaces, either change the IFS variable or use a while read loop, redirecting standard input from <(find Aver*).
Why is the loop traveled only once? If I write $LIST without quotes then the spaces in the filenames make the loop to be traveled once for every word in the filename.
When you store the list of filenames in a variable, there is no way of telling the difference between (1) spaces that were inside filenames, and (2) spaces that were between filenames. They are indistinguishable. A space is a space, it does not have a homoeopathic memory of whether it used to be inside a filename or between two filenames. You can either quote the whole lot as a single string, or not quote it as a list of words. There is no other choice.
The usual ways out of this problem are (a) don't allow spaces in your filenames, (b) use shell filename expansion like eloi showed you, (c) use an array (bash-specific, read the manual), (d) if you are writing a script, set the positional parameters (set -- Aver*) and use them as "$@", (e) use the find command with '-print0' and the xargs command with --null (read the manual for details).
If you are now feeling a bit sick, this is why most people choose option (a).
Note also that if there are *no* matching files, shell filename expansion of 'Aver*' will return the string 'Aver*' and the loop will be executed once with i='Aver*' (which does not exist). This might give the user a helpful error message, or it might cause an unhelpful error. To avoid this, test the value of i within the loop and do nothing if it equals 'Aver*', or set the shell option 'nullglob'.
If you use quotes the whole list is taken as a single word! To avoid problems with blank spaces, either change the IFS variable or use a while read loop, redirecting standard input from <(find Aver*).
while read has its own problems -- it will strip any spaces from the start of each filename and collapse multiple spaces inside filenames
IFS is a game you can't win. Choose any random character that you *can't* have in your filenames. Are you feeling lucky?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.