LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-16-2013, 08:35 PM   #1
davie_93
LQ Newbie
 
Registered: Oct 2013
Posts: 2

Rep: Reputation: Disabled
Grep issue - specific word from line


Hi I'm new here and just needed a small helping hand

The problem is that I am trying to restore a file to a location that it had previously - unfortunately I know what the error is but don't know how to solve it.

for example the file test1 has a directory of /home/dave/test1 it is then moved to a dustbin type feature (directory). When retrieving the file and trying to put it back I cannot as the previous location is /home/dave/test1. Any way to sort of grep/find the test1 out - has to be generic for all files trying to do the same.


Code:
if [ "$1" == "" ]; then
echo "You must enter the file's name you are wanting to restore"

elif [ ! -e "/home/dave/dustbin/$1" ]; then
echo "No file in dustbin named '$1' please check spelling"

else

grou=$(basename "$1") 
location=`grep $1 /home/dave/fileorigin` 
filename=`basename "$location"`	

mv $filename $location

#echo $filename
#echo $location				

fi
 
Old 10-16-2013, 10:33 PM   #2
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
If I understand, there's only one file in the directory and you want to move file
/home/dave/test1/test1 to file /home/dave/test1.

You can do that in a way equivalent to this:

Code:
mv /home/dave/test1 /home/dave/TT
mv /home/dave/TT/test1 /home/dave/test1
rmdir /home/dave/TT
 
Old 10-17-2013, 08:42 AM   #3
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,150

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Cool

Quote:
Originally Posted by davie_93 View Post
Hi I'm new here and just needed a small helping hand

The problem is that I am trying to restore a file to a location that it had previously - unfortunately I know what the error is but don't know how to solve it.

for example the file test1 has a directory of /home/dave/test1 it is then moved to a dustbin type feature (directory). When retrieving the file and trying to put it back I cannot as the previous location is /home/dave/test1. Any way to sort of grep/find the test1 out - has to be generic for all files trying to do the same.


Code:
if [ "$1" == "" ]; then
echo "You must enter the file's name you are wanting to restore"

elif [ ! -e "/home/dave/dustbin/$1" ]; then
echo "No file in dustbin named '$1' please check spelling"

else

grou=$(basename "$1") 
location=`grep $1 /home/dave/fileorigin` 
filename=`basename "$location"`	

mv $filename $location

#echo $filename
#echo $location				

fi
try this: (go to /home/dave directory and move it from there)

cd /home/dave

mv -r ./dustbin /home/dave

Good luck!!!

Last edited by JJJCR; 10-17-2013 at 08:43 AM. Reason: edit
 
Old 10-17-2013, 09:08 AM   #4
davie_93
LQ Newbie
 
Registered: Oct 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
It was kind of a eureka moment I answered my own question just use the opposite of basename when trying to remove the suffix.

Thanks for all the help guys!

Code:
elif [ ! -e "/home/dave/dustbin/$1" ]; then
echo "No file in dustbin named '$1' please check spelling"
else
fio=`grep $1 /home/dave/fileorigin`
location=`dirname "$fio"`
filename=`basename "$fio"`
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
insert a word in a specific line in a file AhmedAmer12 Linux - Newbie 2 07-10-2013 07:08 AM
How to convert specific word into specific colors in console shell? arfal Linux - Server 3 05-10-2012 12:40 AM
[SOLVED] find specific word in a line in a file. Kashif_Bash Programming 5 04-20-2012 06:15 PM
[SOLVED] Find position of specific word in a line rajeshpvndd Linux - Newbie 3 11-05-2011 07:57 AM
how to get a word from a line using sed or grep nagendra Linux - Newbie 4 03-31-2010 01:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:42 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration