Restore files by lines number!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Restore files by lines number!
# 1  
Old 01-05-2008
Error Restore files by lines number!

Hello every 1

Happy new year Smilie

I have a problem when i've tried to write my shell scripts..

this what i've done

Quote:
count=0
for file in `ls -1 $HOME/trash`
do
count=`expr $count + 1`
echo "$count: $file" >> $menu_file
done
echo "Please select a number from this menu"
cat $menu_file

if [ $menu_file = $file ]
then
mv $HOME/trash/$file $file

elif [ -d $HOME/trash/$file ]
then
mv $HOME/trash/$file $file

else
echo "file not found"
fi
Actually, i would like to restore the files that i have deleted by line number for example :

1 : filename1
2: filename2
3: filename3

then i can restore them by line number not by file name

any idea...

thanks in advance Smilie
# 2  
Old 01-05-2008
Hi,
I have just modified your script,

---------
#!/bin/sh

NOTV=64
menu_file=./menufile.out
trap "rm -f $menu_file" EXIT

count=0
for file in `ls -1 ./trash`
do
count=`expr $count + 1`
echo "$count: $file" >> $menu_file
done

cat $menu_file
echo -n "Please select a number from this menu : "
read num

FILE=`sed "/^\<$num\>/!d" menufile.out | awk -F ":" '{print $NF}'| sed 's/^ //'`
[ -z $FILE ] && echo "Not a valid number" && exit $NOTV
echo "Recovering $FILE"
[ -e "./trash/$FILE" ] && mv "./trash/$FILE" . || echo "$FILE is not present in ./trash dir"

-----


Let me know if it works :-)

//Jadu
# 3  
Old 01-06-2008
Thank you very much man Smilie

it's working perfectly Smilie
# 4  
Old 01-06-2008
Quote:
Originally Posted by jaduks
Hi,
count=0
for file in `ls -1 ./trash`
do
count=`expr $count + 1`
echo "$count: $file" >> $menu_file
done
[code]
Take care of files with spaces. Use shell expansion instead
eg
Code:
 for file in ./trash/*

. take note it will not list out hidden files. Use while loop if hidden files are required. eg
Code:
ls -a | while read file ......

# 5  
Old 01-06-2008
Tools

Why do all this manual stuff, when you have "select"?

Code:
#!/bin/ksh

trashdir=./trash
PS3='Enter your choice: '
quitstr=exit
select fname in `ls -1 $trashdir/*` $quitstr ; do
        [ "${fname}" = "${quitstr}" ] || [ "${fname}x" = "x" ] && break
        mv $fname . || echo "Error moving file $fname to `pwd`"
        break
done

In action:

Code:
$ ls -ltr trash
total 0
-rw-r-----   1 sdass    informix       0 Jan  7 02:26 d
-rw-r-----   1 sdass    informix       0 Jan  7 02:26 c
-rw-r-----   1 sdass    informix       0 Jan  7 02:26 b
-rw-r-----   1 sdass    informix       0 Jan  7 02:26 a

$ clean.sh
1) ./trash/a
2) ./trash/b
3) ./trash/c
4) ./trash/d
5) exit
Enter your choice: 99

$ clean.sh
1) ./trash/a
2) ./trash/b
3) ./trash/c
4) ./trash/d
5) exit
Enter your choice: 5

$ clean.sh
1) ./trash/a
2) ./trash/b
3) ./trash/c
4) ./trash/d
5) exit
Enter your choice: 4
$ ls -ltr d
-rw-r-----   1 sdass    informix       0 Jan  7 02:26 d

It is quite compact and clean, builds and shows menu w/o separate coding and has in-built checks for empty/invalid inputs.

HTH
# 6  
Old 01-06-2008
Quote:
Originally Posted by rikxik
Code:
....
select fname in `ls -1 $trashdir/*` $quitstr ; do
....

Code:
...
select fname in $trashdir/* $quitstr 
...

# 7  
Old 01-06-2008
There is a problem with that style if trash is empty:

Code:
$ ls -ltr trash
total 0
$ clean.sh
1) ./trash/*
2) exit
Enter your choice: 2

Of course we can put some additional checks for empty directories - left as an exercise for OP Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print number of lines for files in directory, also print number of unique lines

I have a directory of files, I can show the number of lines in each file and order them from lowest to highest with: wc -l *|sort 15263 Image.txt 16401 reference.txt 40459 richtexteditor.txt How can I also print the number of unique lines in each file? 15263 1401 Image.txt 16401... (15 Replies)
Discussion started by: spacegoose
15 Replies

2. Shell Programming and Scripting

Compare two files and count number of matching lines

Dear All, I would like to compare two files and return the number of matches found. Example File A Lx2 L1_Mus1 L1Md_T Lx5 L1M2 L1_Mus3 Lx3_Mus Lx9 Lx2A L1Md_A L1Md_F2 File B L1_Mus3 L1_Mus3 (3 Replies)
Discussion started by: paolo.kunder
3 Replies

3. Shell Programming and Scripting

[Solved] Script to concatenate 2 files with the same number of lines

Hi everyone, I have two files, namely: file1: file1Col1Row1;file1Col2Row1;file1Col3Row1 file1Col1Row2;file1Col2Row2;file1Col3Row2 file1Col1Row3;file1Col2Row3;file1Col3Row3file2: file2Col1Row1;file2Col2Row1;file2Col3Row1 file2Col1Row2;file2Col2Row2;file2Col3Row2... (0 Replies)
Discussion started by: gacanepa
0 Replies

4. UNIX for Dummies Questions & Answers

Command to split the files based on the number of lines in it

Hello Friends, Can anyone help me for the below requirement. I am having a file called Input.txt. My requirement is first check the count that is wc -l input.txt If the result of the wc -l Input.txt is less than 10 then don't split the Input.txt file. Where as if Input.txt >= 10 the split... (12 Replies)
Discussion started by: malaya kumar
12 Replies

5. Shell Programming and Scripting

Pasting files with different number of lines

Hi all, I tried to use the paste command to paste two files with different number of lines. e.g. file1 A 1 B 1 C 2 D 2 file2 A 2 B 3 C 4 D 4 E 4 (2 Replies)
Discussion started by: f_o_555
2 Replies

6. Programming

perl - calculate the number of lines from particular files

Hi, plz see the below code. here my aim is to calculate the number of lines in unprocessedData.out if this file contains 40 lines then lastly $linenum should print 40.(except blank lines) i have tried below code but it giving me the output only one. can anyone help me how to do ? ... (9 Replies)
Discussion started by: pspriyanka
9 Replies

7. Shell Programming and Scripting

how can i find number of lines in files & subdirectories

how can i find number of lines in files & subdirectories ? (3 Replies)
Discussion started by: pcbuilder
3 Replies

8. UNIX for Dummies Questions & Answers

Read directory files and count number of lines

Hello, I'm trying to create a BASH file that can read all the files in my working directory and tell me how many words and lines are in that file. I wrote the following code: FILES="*" for f in "$FILES" do echo -e `wc -l -w $f` done My issue is that my file is outputting in one... (4 Replies)
Discussion started by: jl487
4 Replies

9. Shell Programming and Scripting

Script to split files based on number of lines

I am getting a few gzip files into a folder by doing ftp to another server. Once I get them I move them to another location .But before that I need to make sure each gzip is not more than 5000 lines and split it up . The files I get are anywhere from 500 lines to 10000 lines in them and is in gzip... (4 Replies)
Discussion started by: gubbu
4 Replies

10. Shell Programming and Scripting

Number of *.txt files which have over n lines?

Can you help me please? I know that wc -l *.txt gives you the number of lines in each file. But how can i count the files that have over n lines? (4 Replies)
Discussion started by: dark_knight
4 Replies
Login or Register to Ask a Question