Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked 1. Related 1. Hot Network Questions. Registered User. Join Date: Jan Join Date: Dec Perhaps this will help. Code :. Join Date: Sep You are the best Shell Programming and Scripting.
Hey guys, I have wrote the following script to apply a module named "trinity" on my files. Add sequence number to files. Hi I am having files in my dir ,some times it vary ,i need to assign sequence number with gunzip the file name. Please advise ex: file1.
Case script to get missing sequence among files. But sometimes you just need a bulldozer, not a shovel. The mv command has a purpose in life, and that is to move files. It is a happy side effect that it can be used to move an existing file into a new file, with a new name. The net effect is to rename the file, so we get what we want. But mv is not a dedicated file renaming tool. To use mv to rename a file type mv , a space, the name of the file, a space, and the new name you wish the file to have.
Then press Enter. Things get trickier when you want to rename multiple files. You must resort to using some nifty Bash tricks. So, yes, it worked. The next part says what the processing will do.
It is using mv to move each file to a new file. It also has a different name in different families of Linux, but they all work the same way. In Fedora and RedHat-derived distributions you install prename like this. To install it in Manjaro Linux use the following command. Note that the renaming command is called perl-rename. The first part is the command name, rename or prename or perl-rename , for the other distributions.
The middle part defines the work we want to be done on each filename. The s means substitute. The first term. In the directory are a lot of C source code files.
We can check this with ls. The format of the command is already familiar to us. We can check the result of that command by repeating the ls command from above with the same parameters:. This will likely not handle special chars correctly. Add a comment. Active Oldest Votes.
Improve this answer. Also, this doesn't do it in creation date order or minimize the padding which are things the OP specified. Needed to double quote wrap the mv for this to work in my bash environment. Could just be Cygwin although it's terminal behaviour is largely identical to normal Unix shells but it seems to have a problem when there are spaces in the filename.
I have lost about files at a glimpse. I think -i should be included in the answer and note should be rewritten accordingly. Show 8 more comments. Community Bot 1 1 1 silver badge. Works Great. Beauty in one line. I added ls -tr get the files by last modified time.
Warning: You might want to add -n to the mv command to prevent accidentally overwriting files. Discovered this the hard way! Show 21 more comments. Pero Pero 1, 17 17 silver badges 17 17 bronze badges. I modified to use row number NR for a shorter command. Massive security vulnerabilities here. Use quotes around the source filename.
The for loop is not taking 1 second per file, strictly speaking. It is taking a long time to fetch the 36, file names from the file system, then loops over them reasonably quickly. Many file systems have issues with large directories, regardless of which precise commands you run; but typically, find is going to be faster than a shell wildcard because it has to fetch and sort the list of matching files.
Pero Your usage of mv is dangerous. Besides CharlesDuffy's objections, what happens if there before running your script already is a file with name It will be silently overwritten by the first file treated.
Secondly, if a file happens to start with a dash, mv treats the rest of the file name as flags. Both problems have been mentioned in the comments to gauteh's answer, as well as their solution: Use mv -i -- instead of mv. Apart from that, I agree that using find is better than using a shell glob, for the reason you have pointed out.
Show 3 more comments. Roy Shilkrot Roy Shilkrot 1, 22 22 silver badges 16 16 bronze badges. Exactly what I was looking for. I like that you can set the start index. Please note this will not keep the original order of the files. RoyShilkrot What do you mean by » will not keep the original order «? Globs in bash and other posix shells expand in sorted order according to the system's locale. Of course, this does not sort 9.
No, this is likely a dangerous solution not to mention the fact that special chars and spaces in file names will not work correctly. I used: find. Like Pero's solution, there are security vulnerabilities here. Thanks, this works. But you should quote the first argument to mv. If you have a file named i. This is what I used: find.
0コメント