Been working since 25+ hrs: Bash Script to rename files supposedly direct but difficult to execute


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Been working since 25+ hrs: Bash Script to rename files supposedly direct but difficult to execute
# 1  
Old 05-30-2012
Been working since 25+ hrs: Bash Script to rename files supposedly direct but difficult to execute

SmilieSmilieSmilie

Hi I have horrible script below, need help in renaming ls -l output into new filename format:

Desired output:

cp -pv original_path/.* newDirectory/owner_of_file.%dd%mm%y.file_extension.first_8_characters_of_original_filename

Code:
 
localuser@localuser:~ vi renamefiles.sh
 
#!/bin/bash
 
a=alice
t=ted
c=carol
b=bob
date=$(ls -l --time-style="+%m %d %y" wiz | egrep -f alice.filenames | grep defs | sed 's/.defs/ /g' | awk '{print $6$7$8}')
mkdir -pv newWiz/large newWiz/small newWiz/alice/large newWiz/alice/small
for largefiles in $(cat alice.large | sed 's/\.defs$//g' | awk '{print $NF}' | cut -c1-8)
do
        echo -e $a$date$largfiles ;
done

---------- Post updated at 01:46 PM ---------- Previous update was at 01:44 PM ----------

Code:
cat list.txt #sample out from ls -l
-rwx------- 1 alice staff 1586 2010-11-05 02:27 request-key.conf -rwx------- 1 ted staff 126 2011-04-29 13:54 cvs-pserver.conf
-rwx------- 1 carol staff 644 2011-03-06 21:58 ts.conf

---------- Post updated at 01:47 PM ---------- Previous update was at 01:46 PM ----------

so far I have extracted the date, owner, and first 8 characters

However I figured out how can I combine them accordingly without overlapping each output results??? Smilie

Last edited by wolf@=NK; 05-30-2012 at 06:14 PM.. Reason: new title
# 2  
Old 05-30-2012
I don't know how you expect to avoid duplicates with that naming scheme. If there are duplicates, what should be done about it?
# 3  
Old 05-30-2012
duplicates is ok as long as I can produce those files

disregard echo line

I think it should be
Code:
 touch $owner$modifieddate$file_extension$truncatedfilename

# 4  
Old 05-30-2012
How about something like this:

Code:
stat --format='%U %Y %n' wiz/* | while read uid time name
do   
  fname=$(basename $name)
  prefix=${fname%.*}  
  ext=${fname##*.}
  newname=$uid.$(date -d @$time +%d%m%Y).${ext}.${prefix:0:8}
  echo cp $name newWiz/$newname
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop in bash - Direct output to two files

Hello all, i have a code in which when doing a for loop, i need to direct the output to two files, one just a single output, the other to always append (historical reasons). So far i managed to do the following, which is working, but am still considering it as "dirty". ... (4 Replies)
Discussion started by: nms
4 Replies

2. Shell Programming and Scripting

Adding a cron job that will execute everyday at 14:50 hrs

Hi I want to execute a cron job everyday at 14:50 hrs. I have a script like this: TMP_FILE="/tmp/tmpcron.txt" RES=0 /usr/bin/crontab -l >> $TMP_FILE ADD_JOB="50 14 * * * /opt/mypath/scriptname.sh" grep "scriptname.sh" $TMP_FILE >> /dev/null JOB_NOT_EXIST=$? if test $JOB_NOT_EXIST... (2 Replies)
Discussion started by: saurabhkoar
2 Replies

3. UNIX for Dummies Questions & Answers

Bash script to execute a program to rename files

I just can't figure it out , so please just give me a pice of advise how to: The existing Linux program foo2bar takes as its only argument the name of a single foo file and converts it to an appropriately-named bar file. Provide a script that when executed will run foo2bar against all foo... (4 Replies)
Discussion started by: raymen
4 Replies

4. UNIX for Dummies Questions & Answers

Bash script to rename files in a directory

Dear friends, I have created a script to rename all files in a directory by appending the file name with username (who created the file), the date it was created. For example, "apple.doc" should be renamed to "johnFeb23apple.doc" where "john" is the owner and "Feb23" is file created date. It... (4 Replies)
Discussion started by: djsnifer
4 Replies

5. Shell Programming and Scripting

Difficult problem: Complex text file manipulation in bash script.

I don't know if this is a big issue or not, but I'm having difficulties. I apoligize for the upcoming essay :o. I'm writing a script, similar to a paint program that edits images, but in the form of ANSI block characters. The program so far is working. I managed to save the image into a file,... (14 Replies)
Discussion started by: tinman47
14 Replies

6. UNIX for Dummies Questions & Answers

Bash script to rename all files within a folder...

Hi. I don't have any experience with making scripts in bash. I need a simple script to rename all files in a folder to the format file1.avi, file2.avi, file3.avi, and so on..... Please note that the original files have different filenames and different extensions. But they all need to be... (2 Replies)
Discussion started by: dranzer
2 Replies

7. Shell Programming and Scripting

Moving old files bash script - not working properly

I'm trying to write a script that moves data that's older than 2 weeks to a different place. It works well, EXCEPT, that when the script hits a file within a directory inside the working directory, it will move it to the root of the destination directory instead of putting it in the correct... (1 Reply)
Discussion started by: ugolee
1 Replies

8. Shell Programming and Scripting

stdout redirect is working buy direct script exec but not in cron

Hi @ all :) i made a very little shell script witch is working well when i'm launching it directly like with ./script but when i'm launching it by cron tab it work at half only. the part of the script witch are not working are: #!/bin/sh apt-get updade apt-get -s upgrade >>... (5 Replies)
Discussion started by: calibal
5 Replies

9. Shell Programming and Scripting

Hello - new here - bash script - need to rename and zip files.

I'm working on a project that basically unzips three zip files. When these unzip they create about 70+ directories with subdirectories of year/month with about 3 to 9 pdf files in each directory. Basically, I'm needing to figure out a way to zip these pdf files up. for instance the script... (1 Reply)
Discussion started by: Aixia
1 Replies

10. Shell Programming and Scripting

Shell script is taking more than 3 hrs to execute

Hi I am doing a process of converting all the values of key column into a row, for eg Key col1 col2 1 1 1 1 2 1 1 1 3 1 3 1 2 ... (2 Replies)
Discussion started by: nvuradi
2 Replies
Login or Register to Ask a Question