Hey guys,
ive been working on this for about 2hrs now - without any solution.
At first I need to say I dont have skills in linux bash scripting, but I tried to use some codesnippets and manuals from google.
What I want to do:
I have different folders including 2 different filestypes with diffent names, ex. /folderxyz/importantname.txt , /folderxyz/file.cad
I want the file.cad named to importantname.cad (so name without ending from txt file), moving the renamed file to a different folder and delete the folder of these two files.
I know I need to use some kind of loop, but I didnt even get the code to rename the file. I startet getting the folders with
find -name *.txt
but I didnt get managed to handle the string to seperate the name (for rename) and the dir to change the directory.
(1) Do you have only two files in the directory "folderxyz", as indicated in your post?
(2) Or do you have more than two?
(3) In case you have more than two, then what is the relationship between a "cad" file name and a "txt" file name it should be converted to? For example, if the directory "folderxyz" has 4 files: "foo.txt", "bar.cad", "fizz.txt" and "buzz.cad", then should "buzz.cad" be renamed to "fizz.cad" or "foo.cad"?
Repeating some of what durden_tyler said and adding some additional notes: You need to be much clearer about what you are trying to do and what you have done.
The find utility is used to search a file hierarchy for various criteria. In the sample you provided, you will get a syntax error if the directory in which you run this command contains more than one file with a name ending with the string .txt , will search the file hierarchy for a file with the same name as the file in current directory with a named ending in .txt if there is exactly one file in the current directory with a name ending in .txt , or will find all of the files in the file hierarchy rooted in the current directory with names ending in .txt if there aren't any files in the current directory with a name ending with .txt .
Once you have found a file with a name ending in .txt , how do you determine which file with a name ending in .cad should be renamed?
After renaming that .cad file (and moving or copying it to another directory), do you really want to remove every file in the current directory (including the .txt that you haven't moved or copied to anywhere else) even if there are other .txt and/or .cad files remaining in that directory or in subdirectories under that directory?
And, to what directory do you want to move the renamed .cad files?
We'll be happy to help you figure out how to write a bash script that will run on a Linux operating system if you give us a clear description of what you're trying to do. Without a clear description, we can make wild guesses that could waste a lot of your time and ours.
there a different types of files, even subfolders, but just only one .txt and .cad
sure, i try to give my best explaning my plans in english.
i try to explain this by creating a structure with all possibilities:
myrootfolder/folder1/ #no .cad and no .txt file - can be ignored
myrootfolder/folder2/ #including .txt or .cad, but not both - can be ignored
myrootfolder/folder3/ #including both files, need to rename the .cad, move the cad to myrootfolder/finished/ and remove folder3
or
myrootfolder/folder4/folder4sub/ #including both files, need to rename the .cad, move the cad to myrootfolder/finished/ and remove folder4
yes, this procedure for every folder (or subfolder)
not always, there are folders with different files too, but they should stay untouched only performing actions on folders that contains .txt and .cad
Sorry, I had a few days off.
So what I mean is, that this "folder" or "folder3" is not always the same name. Its name could be like "xyz" and the next folder is like "12345".
What I understand is, the first line is the rename process, but in the 2nd line you wrote "/folder3/folder3sub/file.cad", but this should be done with variables, caus the whole path could be different, so even the rmdirpath.
The second and third line are the result of the first, echo ing what would be done (instead of doing it). Modify the start folder in the find command, run the short script and report back on the results.
and this seems to work fine
what were these /*/* for?
---------- Post updated at 09:58 AM ---------- Previous update was at 09:33 AM ----------
Okay, i dont want to edit my last post again.
So for now its working with 2 problems:
directories with *.txt and without *.cad are deleted too (should not happen)
and
if im in a subdirectory, the mainfolder is not deleted, just the subdirectory
That scriptlet was not intended to run in ./ but on subdirectories below it. /*/* expands to two directory levels below . .
You could rm the .txt file IF the .cad file has been mv ed successfully; then rmdir should work correctly , too.
Why should the main folder be deleted?
After some hours of getting into this bash stuff, I got my script finally working, as I want it. Even my script looks pretty different to yours I build it up on your "-name
-name "*.txt" | while read FN; do PT=${FN%/*}/; FN=${FN#$PT};
i think this might be a very dirty solution, so not like someone should learn from that, its somehow working now, but there are still some things im not happy with
#!/bin/sh
cd "/i-data/64903fcc/";
find ./*/ ! -path "*/finished/*" ! -ipath "*/text/*" -name "*.cad" ! -iname "*text*" | while read FN;
do
PT1=${FN%/*}; #Kompletter Pfad ohne Dateinamen
PT2=${PT1#*/}; #kompletter Pfad ohne ./ am Anfang und ohne / am Ende
PT3=${PT2%%/*}; #Main Folder, f�r Trash
mv --backup=numbered "${FN}" "./finished/${PT1##*/}.cad"; echo moved "${FN}" "-->" "./finished/${PT1##*/}.cad";
mv --backup=numbered "./${PT3}" "./Trash/"; echo trashed "./${PT3}";
done
so as i post this here, i got a few more questions:
how can i handle this procedure to stop, when there are more than 1 .cad files in 1 folder? (in my case, it would handle one and delete the rest)
is there a better way of logging the commands instead of my version with echo and >> logfile.log (so i might be to able to log errors aswell?)
how to clear items in trash, older than 7 days (i might checked them for errors inbetween), ive alread tried this with find, but the -delete does not work, if the folders are not empty