LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Moving files from folders and subfolders to a specific folder (https://www.linuxquestions.org/questions/linux-newbie-8/moving-files-from-folders-and-subfolders-to-a-specific-folder-827615/)

mrj2 08-21-2010 05:22 AM

Moving files from folders and subfolders to a specific folder
 
I am new to bash. I am using secure delete to remove files from a Debian Linux PC. However, secure delete does not remove folders. This has lead me to look at writing a script that would move files to a predetermined folder for deletion. My plan is as follows:

I have a folder on my desktop called shredder where I move the contents of the waste bin to. The script needs to identify all files within the folders and sub folders, within the shredder folder, and move each file to the shredder folder and then delete the folder. At this point secure delete can be used with a command like

shred -v -u *.*

on the shredder folder.

The problem I have is in creating the code to move files from the different folders and then deleting the folders. Note that the names of the files, folders and subfolders will not always be known.

Any help greatly appreciated.

Michael

konsolebox 08-21-2010 05:27 AM

Why not use cp -a then delete the folders?

junkaip 08-21-2010 05:45 AM

thanks...bro

grail 08-21-2010 06:15 AM

Please mark as SOLVED if you have your solution.

onebuck 08-21-2010 07:22 AM

Hi,

Welcome to LQ!

Why not just use the 'mv' command. Just move the parent directory that you desire to remove to the 'shred' directory.

Just a few links to aid you;

Linux Documentation Project
Rute Tutorial & Exposition
Linux Command Guide
Ultimate Linux Newbie Guide
LinuxSelfHelp
Getting Started with Linux
Bash Reference Manual
Advanced Bash-Scripting Guide
Linux Home Networking
Virtualiation- Top 10

:hattip:
The above links and others can be found at 'Slackware-Links'. More than just SlackwareŽ links!

konsolebox 08-21-2010 07:37 AM

I think I misread the point. Please ignore my suggestion and follow onebuck's. In your shedder folder, do
Code:

find -type f -exec shred -v -u {} \;
rm -fr *  # this command will only remove directories since the files are already deleted


mrj2 08-21-2010 11:59 AM

Quote:

Originally Posted by onebuck (Post 4073234)
Hi,

Welcome to LQ!

Why not just use the 'mv' command. Just move the parent directory that you desire to remove to the 'shred' directory.

Just a few links to aid you;

Linux Documentation Project
Rute Tutorial & Exposition
Linux Command Guide
Ultimate Linux Newbie Guide
LinuxSelfHelp
Getting Started with Linux
Bash Reference Manual
Advanced Bash-Scripting Guide
Linux Home Networking
Virtualiation- Top 10

:hattip:
The above links and others can be found at 'Slackware-Links'. More than just SlackwareŽ links!

I want to put this in a script. I will not always know the names of the folders and files unless I manually look. I am attempting to create a black box where the script does not need to know the names of the files and folders; eliminating the need to do the manual work myself. Using mv outside of some programming structure will only work if I do the work manually.

mrj2 08-21-2010 12:09 PM

Quote:

Originally Posted by konsolebox (Post 4073245)
I think I misread the point. Please ignore my suggestion and follow onebuck's. In your shedder folder, do
Code:

find -type f -exec shred -v -u {} \;
rm -fr *  # this command will only remove directories since the files are already deleted


Thanks for you response. I have run the first line of code from the terminal and get an error message :

find: missing argument to `-exec'

Am I correct in that what you are thinking of here is to run your code from a script that that works from the shredder folder. As a result, the files are found in all sub folders and moved to the shredder folder. After this action, the folders are deleted. If that is the case, we are definitely on the right track.

konsolebox 08-21-2010 05:45 PM

Quote:

Originally Posted by mrj2 (Post 4073408)
find: missing argument to `-exec'

I think you forgot to include '\;'. It's crucial. Note that it should be escaped:
Code:

find -type f -exec shred -v -u {} \;
I think it's also better to just copy this whole code to a script file. Placing it in ~/ like ~/shred.sh:
Code:

#!/bin/bash
find -type f -exec shred -v -u {} \;
rm -fr *

Then at the shredder folder, run
Code:

bash ~/shred.sh

mrj2 08-22-2010 08:57 AM

Quote:

Originally Posted by konsolebox (Post 4073612)
I think you forgot to include '\;'. It's crucial. Note that it should be escaped:
Code:

find -type f -exec shred -v -u {} \;
I think it's also better to just copy this whole code to a script file. Placing it in ~/ like ~/shred.sh:
Code:

#!/bin/bash
find -type f -exec shred -v -u {} \;
rm -fr *

Then at the shredder folder, run
Code:

bash ~/shred.sh

Thank you for your code. I ran it from a script. I wasn't quite sure what you meant by ~/ I put the file in the Shredder folder. I am delighted that all the files are deleted. In fact I am amazed how such a short line of code could produce such results. I am definitely interested in learning bash!!

One small issue is that the folders are not removed. What happens is the script is removed and the folders stat in tact.

konsolebox 08-22-2010 09:14 AM

I'm not sure what could have really happened but since you placed the script in the shredder folder, it should have been included in the deletion as well.

Perhaps bash no longer continued to run after it finds out that the script no longer exists so it didn't run the second statement that deletes the directories.

~ is just an alias to your home directory. For example if your username is 'user' then your home directory would probably be /home/user. And if that's the case, if you try to run 'cd ~', it will just be the same as 'cd /home/user'. Running 'cd' with no argument is also the same as 'cd ~' or 'cd /home/user'.

What I meant before was that you place the script in your home directory.

konsolebox 08-22-2010 09:22 AM

You should be careful not to run the script anywhere but in the shredder directory. I'll make some modifications to the script just to be safe:
Code:

#!/bin/bash

# change this to the path where shredder is located

SHREDDERDIR=/home/$USER/Desktop/shredder

if [[ ! $PWD = "$SHREDDERDIR" ]]; then
    echo "This is not the shredder directory!"
else
    find -type f -exec shred -v -u {} \;
    rm -fr *
fi


mrj2 08-22-2010 12:40 PM

Quote:

Originally Posted by konsolebox (Post 4074120)
You should be careful not to run the script anywhere but in the shredder directory. I'll make some modifications to the script just to be safe:
Code:

#!/bin/bash

# change this to the path where shredder is located

SHREDDERDIR=/home/$USER/Desktop/shredder

if [[ ! $PWD = "$SHREDDERDIR" ]]; then
    echo "This is not the shredder directory!"
else
    find -type f -exec shred -v -u {} \;
    rm -fr *
fi


What can I say except a huge thankyou. I have tested the script thoroughly and it performs exactly as expected. This is a very useful facility.

Michael


All times are GMT -5. The time now is 05:14 AM.