LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 08-21-2010, 05:22 AM   #1
mrj2
LQ Newbie
 
Registered: Aug 2010
Posts: 5

Rep: Reputation: 0
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
 
Old 08-21-2010, 05:27 AM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,243
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Why not use cp -a then delete the folders?
 
Old 08-21-2010, 05:45 AM   #3
junkaip
LQ Newbie
 
Registered: Aug 2010
Posts: 1

Rep: Reputation: 0
thanks...bro
 
Old 08-21-2010, 06:15 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,412

Rep: Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874
Please mark as SOLVED if you have your solution.
 
Old 08-21-2010, 07:22 AM   #5
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 11,022
Blog Entries: 1

Rep: Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364
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


The above links and others can be found at 'Slackware-Links'. More than just SlackwareŽ links!
 
Old 08-21-2010, 07:37 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,243
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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
 
Old 08-21-2010, 11:59 AM   #7
mrj2
LQ Newbie
 
Registered: Aug 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by onebuck View Post
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


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.
 
Old 08-21-2010, 12:09 PM   #8
mrj2
LQ Newbie
 
Registered: Aug 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by konsolebox View Post
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.
 
Old 08-21-2010, 05:45 PM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,243
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by mrj2 View Post
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

Last edited by konsolebox; 08-21-2010 at 05:50 PM.
 
Old 08-22-2010, 08:57 AM   #10
mrj2
LQ Newbie
 
Registered: Aug 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by konsolebox View Post
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.
 
Old 08-22-2010, 09:14 AM   #11
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,243
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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.
 
Old 08-22-2010, 09:22 AM   #12
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,243
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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
 
Old 08-22-2010, 12:40 PM   #13
mrj2
LQ Newbie
 
Registered: Aug 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by konsolebox View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Samba folder containing subfolders with specific rights StarQuake64 Linux - Server 4 06-05-2009 12:05 PM
find all files in current folder and all subfolders and sort all by timestamp dlnlinux Linux - Newbie 2 06-03-2008 11:29 AM
need a script to delete all files from a folder and subfolders cccc Programming 1 03-04-2005 10:54 AM
changeing permissions for a folder + subfolders and files ? YBA^[x] Slackware 7 08-06-2004 04:07 AM
moving files and folders thru command line JROCK1980 Linux - General 2 02-16-2004 06:55 PM


All times are GMT -5. The time now is 06:29 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration