Sponsored Content
Top Forums Shell Programming and Scripting Rename folder/directory after running sort script Post 302412863 by jl487 on Tuesday 13th of April 2010 07:33:40 PM
Old 04-13-2010
Rename folder/directory after running sort script

Hello, I'm trying to create a script that sorts documents by author (found in file name) and then create a directory for that author. For example,

Input:
John - Paper_1.txt
John - Paper_2.txt
Mark - Paper_1.txt
Jill - Paper_1.txt

Output:
dir/John/Paper_1.txt
dir/John/Paper_2.txt
dir/Mark/Paper_1.txt
dir/Jill/Paper_1.txt


I have the following code, but the directories are named very strangely (possibly because of the sort feature I used).

HTML Code:
for file in *.txt
do
  dir=`find *txt | cut -d ' ' -f 1 | sort -u`
  mkdir -p "$dir"
  mv "$file" "$dir"
done
Right now, the code creates directories with the following names:
dir/John Mark Jill/Paper_1.txt
dir/John Mark Jill/Paper_2.txt
dir/Mark Jill/Paper_1.txt
dir/Jill/Paper_1.txt

Can anyone help?

---------- Post updated at 07:33 PM ---------- Previous update was at 07:02 PM ----------

As another route, I know that I can create the directories first, by using
Code:
 mkdir $(ls *txt | cut -d - -f1 | sort -u)

but then the issue of sorting the files arises.

Any suggestions?

Last edited by jl487; 04-13-2010 at 10:37 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple BASH shell script to rename webcam jpg and copy into a new directory.

System: Ubuntu Intrepid Ibex I'm running webcamd as a sort of "security" program, but I need a script that will archive my webcam.jpg files. So, take the following file: /home/slag/www/webcam.jpg Rename it--preferably with a time stamp. Place it in say: /home/slag/www/history/ ... (4 Replies)
Discussion started by: robfindlay
4 Replies

2. Shell Programming and Scripting

Unique Directory and Folder Deletion Script

Ok, so I just got charged with the task of deleting some 300 user folders in a FTP server to free up some space. I managed to grep and cut the list of user folders to delete into a list of one user folder per line. Example: bob00 jane01 sue03 In the home folder, there are folders a-z, and... (5 Replies)
Discussion started by: b4sher
5 Replies

3. Shell Programming and Scripting

Running a script with all files in a folder

Hi I have many files in a folder (like 200) and I want to run a Perl script through all of them. One way would be to do it one by one (which will take forever). But I am sure there is a faster way of doing it. So inside the folder i have files named in no particular way eg. YUI456... (3 Replies)
Discussion started by: phil_heath
3 Replies

4. 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

5. Shell Programming and Scripting

sed to rename files in a folder - please help with script

Hello, I am new to shell scripting and stuck on renaming files in a folder. The files have the format chp01_00001.wav chp01_00002.wav .... chp02_00001.wav chp02_00002.wav .... but I want them to have the following names: chp_bloomy_00001.wav chp_bloomy_00002.wav chp_bloomy_00003.wav... (8 Replies)
Discussion started by: Bloomy
8 Replies

6. Shell Programming and Scripting

URGENT!!! bash script to sort files into folder according to a string in the filename

Hi all. I am very new to linux scripting and i have a task i can only solve with a script. I need to sort files base on the date string in their filenames and create a folder using the same date string then move the files to their respective folders. Scenario: Folder Path:... (1 Reply)
Discussion started by: ace47
1 Replies

7. Shell Programming and Scripting

Bash script to sort files into folder according to a string in the filename

Hi all. I am very new to linux scripting and i have a task i can only solve with a script. I need to sort files base on the date string in their filenames and create a folder using the same date string then move the files to their respective folders. Scenario: Folder Path:... (1 Reply)
Discussion started by: ace47
1 Replies

8. 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

9. Shell Programming and Scripting

Shell Script to Enter a Directory (Folder Name Contains Spaces)

I am writing a shell script to cd to a folder. folder name is "October Scripts" I am currently storing the name of folder in a shell variable as so path="October\ Scripts/". If I do cd $path I receive the error bash: cd: October\: No such file or directory What am I doing wrong? Kindly... (3 Replies)
Discussion started by: anand2308
3 Replies

10. Shell Programming and Scripting

Need help in writitng a script to rename file name and copy to other folder

Hi All, My requirement is as follows: A file (say abc) will be having list of the .txt file names. I need to read this abc file line by line and rename the .txt file names inside it and move them to other folder/path. Eg: abc ------- file1.txt file2.txt file3.txt Output (should... (1 Reply)
Discussion started by: pavan.yadalla
1 Replies
MasonX::ProcessDir(3pm) 				User Contributed Perl Documentation				   MasonX::ProcessDir(3pm)

NAME
MasonX::ProcessDir - Process a directory of Mason 2 templates VERSION
version 0.02 SYNOPSIS
use MasonX::ProcessDir; # Generate result files in the same directory as the templates # my $pd = MasonX::ProcessDir->new( dir => '/path/to/dir' ); $pd->process_dir(); # Generate result files in a separate directory # my $pd = MasonX::ProcessDir->new( source_dir => '/path/to/source/dir', dest_dir => '/path/to/dest/dir' ); $pd->process_dir(); DESCRIPTION
Recursively processes a directory of Mason 2 templates, generating a set of result files in the same directory or in a parallel directory. Every file with suffix ".mc" will be processed, and the results placed in a file of the same name without the suffix. ".mi", autobase and dhandler files will be used by Mason when processing the templates but will not generate files themselves. For example, if the source directory contains Base.mc httpd.conf.mc proxy.conf.mc etc/crontab.mc blah.mi somefile.txt and we run my $pd = MasonX::ProcessDir->new( source_dir => '/path/to/source/dir', dest_dir => '/path/to/dest/dir' ); $pd->process_dir(); then afterwards the destination directory will contain files httpd.conf proxy.conf etc/crontab somefile.txt where foo and bar are the results of processing foo.mc and bar.mc through Mason. Base.mc and blah.mi may be used during Mason processing but won't generate result files themselves. This class is a convenience extension of Any::Template::ProcessDir. CONSTRUCTOR
Specifying directory/directories o If you want to generate the result files in the same directory as the templates, just specify dir. my $pd = MasonX::ProcessDir->new( dir => '/path/to/dir', ... ); o If you want to generate the result files in a separate directory from the templates, specify source_dir and dest_dir. my $pd = MasonX::ProcessDir->new( source_dir => '/path/to/source/dir', source_dir => '/path/to/dest/dir', ... ); Mason options mason_options An optional hash of options to the Mason interpreter. For example, the default Mason data directory will be ".mason" under the source directory, but you can override this: mason_options => { data_dir => '/path/to/data/dir' } Options inherited from Any::Template::ProcessDir See Any::Template::ProcessDir for other options, such as dir_create_mode file_create_mode readme_filename SUPPORT AND DOCUMENTATION
Bugs and feature requests will be tracked at RT: http://rt.cpan.org/NoAuth/Bugs.html?Dist=MasonX-ProcessDir bug-masonx-processdir@rt.cpan.org The latest source code can be browsed and fetched at: http://github.com/jonswar/perl-masonx-processdir git clone git://github.com/jonswar/perl-masonx-processdir.git SEE ALSO
Mason, Any::Template::ProcessDir AUTHOR
Jonathan Swartz <swartz@pobox.com> COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Jonathan Swartz. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.14.2 2012-03-07 MasonX::ProcessDir(3pm)
All times are GMT -4. The time now is 10:40 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy