Visit Our UNIX and Linux User Community


Single command to delete a directory

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Single command to delete a directory
# 1  
Old 10-13-2009
Single command to delete a directory

Use and complete the template provided. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

Write a command that will delete the john directory, and all of its contents. The problem I can't firgure out is how to create a absoute path to the john directory. I'm in the paul working directory which is cd ../john away, so the paul and john directorys are children directorys to the home directory

2. Relevant commands, code, scripts, algorithms:

rm -r john

3. The attempts at a solution (include all code and scripts):

rm -r john ../john

4. School (University) and Course Number:UWM Into to unix
# 2  
Old 10-13-2009
Hi.

You are close, but need to force it (as well as simply recursing it)

Do you need to specify the absolute path? ../john would be enough if you're in the paul directory, and they're both in the home directory.

Code:
$ pwd
/home/paul
$ ls -d ../john
drwxr-xr-x 4 john students     4096 Aug 28 12:18 john


Last edited by Scott; 10-13-2009 at 06:28 PM..
# 3  
Old 10-13-2009
I tried that I prolly didnt explain it very well I'm in the Paul directory so I have to go up one to the home directory than down one to the John directory and delete it, with all its contents
# 4  
Old 10-13-2009
Going back to your original question, an absolute path is one which starts with / (the root directory)

If your directory is /home/paul and john is ../john from where you are, then john is in /home/paul/../john.

Code:
/home/paul # pwd
/home/paul
/home/paul # cd /home/paul/../john
/home/john # pwd
/home/john

.. means parent, therefore john is in the parent of paul, which is /home

i.e /home/john

It's probably best if you delete john from /home instead of /home/john

Code:
cd /home
rm -rf john

or as you have to move up one (..)

Code:
cd ..
rm -rf john

If we decide to start handing out medals for who can explain things stupidly, I must be a contender!
# 5  
Old 10-13-2009
It has to be all in one command so I cant cd.. than rm -r john
# 6  
Old 10-13-2009
Then don't

Code:
cd ..
rm -rf john

is the same as

Code:
rm -rf ../john

Or if you have to use an absolute path, given what you know

Code:
rm -rf /home/john

(but I should add, that only root would normally be allowed to remove john!)

Last edited by Scott; 02-21-2010 at 03:53 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #308
Difficulty: Easy
The first computer bug was created by a broken keyboard input.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Move multiple files to different directory using a single command

I have multiple files test1, test2, test3 etc. I want to move to a different directory with ABC_ prefixed to every file and and current dat time as postfix using a single command. (I will be using this is sftp with ! (command for local server). I have tried the following but it gives error ... (5 Replies)
Discussion started by: Soham
5 Replies

2. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

3. Shell Programming and Scripting

Command to delete half of files in directory.

Hello Friends, I have directory called /tmp. which stores the log files. Whenever it becomes full, i want to delete half of files from all log files. even after deleting the files, if space is more than 90% then it should delete rest of half files. While deleting files, older files... (7 Replies)
Discussion started by: Nakul_sh
7 Replies

4. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

5. Shell Programming and Scripting

Change to directory and search some file in that directory in single command

I am trying to do the following task : export ENV=aaa export ENV_PATH=$(cd /apps | ls | grep $ENV) However, it's not working. What's the way to change to directory and search some file in that directory in single command Please help. (2 Replies)
Discussion started by: saurau
2 Replies

6. Shell Programming and Scripting

Delete empty files from a directory entered in command prompt

My code to "Delete empty files from a directory entered in command promt" #/bin/sh echo "Enter directory" read gh for file in `ls $gh` do # to get the size of file a=$( ls -l file | awk ' {print $7} '); echo $a if then echo "removing file " rm file fi done (6 Replies)
Discussion started by: adirajup
6 Replies

7. UNIX for Dummies Questions & Answers

single line command to delete a 6 months old file

i had this scenario where i need to delete a file that is 6 months old which is no longer needed. basically the filename is in the format of PCARDDAILYmmddyyyy.txt where the mm is the month, dd is the day, and yyyy is the year. e.g. PCARDDAILY05262009.txt PCARDDAILY05252009.txt ... (6 Replies)
Discussion started by: wtolentino
6 Replies

8. Shell Programming and Scripting

shell script which will delete every thing under a directory except a single sub dire

write a shell script which will delete every thing under a directory except a single sub directory (2 Replies)
Discussion started by: alokjyotibal
2 Replies

9. Solaris

How to delete Directory and inside files using Find command

I am using the following Command to delete Directory with contents. But this command is deleting inside files only not directories. is there any change need in my command? find -type f -mtime +3 -exec rm -r {} \; Thanks (3 Replies)
Discussion started by: bmkreddy
3 Replies

10. Linux

command in linux to delete a directory

hi all, can anybody tell me command(in linux) to delete a directory which have other directories & files in it. its urgent thanks vikas (2 Replies)
Discussion started by: guptavikas1
2 Replies

Featured Tech Videos