General discussion

Locked

Shell Script question

By eric_7178 ·
Greetings all,
I need a little help,
I am trying to creating a series of prototypes for a program I am working on.
Question:
I need to record file names and archive the file names in a text file.
In other words:
1. Find a specific file type
2. copy file name to a string
3. save the file name string to a file
any assistance will be greatly appreciated
thank you

This conversation is currently closed to new comments.

2 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Simple algorithm

by jmgarvin In reply to Shell Script question

grep foo bar >> savedstring.txt

Is that what you need?

Collapse -

Examples

by stress junkie In reply to Shell Script question

Your request is extremely vague. Here are some ideas.

1. File type: Do you mean a particular file name extension or a file type such as a directory vs. an ASCII file?

To find a particular file name extension such as .doc then use the ls command as in:
ls *.doc

To find a particular file type such as a regular file use the find command as in:

find . -type f

2. You can put a file name into a variable using the assignment operator (equals sign). If you have a lot of files then you will end up with an array. Here is some example code:
=====
$
$ unset TEST
$ touch fileone.txt
$ touch filetwo.doc
$ touch filethree.xls
$ touch filefour.exe
$ TEST=(`find .`)
$ echo ${TEST[0]}
.
$ echo ${TEST[1]}
./fileone.txt
$ echo ${TEST[2]}
./filetwo.doc
$ echo ${TEST[3]}
./filethree.xls
$ echo ${TEST[4]}
./filefour.exe
$ echo $TEST
.
$
=====
$
$ unset TEST
$ TEST=(`ls -tr .`)
$ echo ${TEST[0]}
fileone.txt
$ echo ${TEST[1]}
filetwo.doc
$ echo ${TEST[2]}
filethree.xls
$ echo ${TEST[3]}
filefour.exe
$ echo ${TEST[4]}
$
=====
You can play with those ideas.

3. You can use a pipe to create and append a value into a file on disk as follows:

echo This line creates the file. > file.txt
echo This line is appended to the file. >> file.txt
echo Another line appended to the file. >> file.txt


I hope this helps. :-)

Back to Linux Forum
2 total posts (Page 1 of 1)  

Related Discussions

Related Forums