Shell rename with first 4 characters
Is there any way for me to use the rename command to rename selected files (in Nautilus - which means I'd have to put the script into my /.gnome2/nautilus-scripts folder) to the first four characters of their current name? So if for instance I had a file called "d3c999cab6273bb86a9a62406993aa1f.doc", it would rename it to just "d3c9.doc", automatically?
Thanks, Steve |
YMMV(VM), completely untested and I don't use the 'rename' command as you required. Maybe some script like:
Code:
#!/bin/sh |
Thanks, but cannot get it to work and cannot see anything obviously wrong, except "FI" instead of "IF" in one of the lines. Can you have a quick scan through please as Bash is not a language I am familiar with.
Thank you again. Steve |
Quote:
Quote:
|
Try:
Code:
for i in *.doc; do mv "$i" "$(echo "$i" | cut -c 1-4).doc"; done |
The "fi" is not a mistake, it closes the block started by "if".
Code:
#!/bin/sh |
Quote:
Quote:
Quote:
Thanks everyone, Steve. |
An adaptation of H_TeXMeX_H's script:
(Note that I don't guarantee that it works, I'm also not too great at bash scripting) Code:
for file in *.*; do # iterate over all files with a dot in their name, but not starting with a dot |
I was playing around and made this command, which will generate the right filename when placed in the script in my previous post in place of the red text:
Code:
echo "$file" | sed --regexp-extended 's:(.{1,4}).*(\.[^.]*):\1\2:' Code:
#!/bin/bash |
It appears to work:
Code:
$ ls |
Quote:
Although it does rename all files in the current directory. Is there an easy change to make it just do the files I have selected? Sort of "for file in selection"? I have a different script that appears to indicate that "$@" means "selected files", but cannot get "for file in $@" to work... Steve Fussy bugger |
Quote:
Code:
c@CW8:~$ function fun { |
Well, I'm taking the $@ idea from a short ps2pdf script, that goes:
cd $NAUTILUS_SCRIPT_CURRENT_URI for arg in $@ do ps2pdf $arg done Its purpose, of course, is to activate ps2pdf for the currently selected file in Nautilus, without having to call up the shell and type "ps2pdf myfile.ps" manually. This script is used by just selecting the .ps file you have in Nautilus, then playing the script. However, is the very first line relevant here? Is its purpose to pass the currently selected filename to the script? And, more importantly, can the "rename to first 4 chars" script in its current form handle looping around all currently selected filenames, i.e. multiple filenames in current selection rather than just one currently selected file? I kinda want to do (I'll revert to a BASIC syntax here): For Each file IN Nautilus.selection (Reduce filename to 4 chars, while keeping extension) End For The previous script from MTK358 works perfectly, but it does For Each file IN Current_Directory, and there will be files in there that I don't want reduce to first_four_chars. The only alternative is to temporarily copy the target files in their own directory first, then play the script, then copy them back again. So the ability to perform the script only on the selected files would be handy. Thank you for any help you can give! Steve |
You should be able to substitute 'for file in *.*' with 'for file in $@' and then supply filenames on the command line as parameters, or you could put the filenames you want to process in a file, one per line, and then read from that file using 'while read file; do [commands go here]; done << filelist' or you could amend the pattern in the original (*.*) to reflect what you want to select (e.g. d*.doc)
|
I'm no expert on bash scripting here, but I take it that "$@" is basically representative of the entire string of characters that comes after the script name on the command line? Does it include spaces? I was just thinking you could maybe use sed or awk or something to chop up the string in "$@" to pieces that are a bit more digestable by the rest of the script...?
Just my :twocents: :scratch: |
All times are GMT -5. The time now is 06:08 AM. |