Results 1 to 6 of 6
Hi guys
I have around 600 empty text files that I need to add the name of this file as part of the data, I mean
files from "file1.txt to ...
- 03-21-2010 #1Just Joined!
- Join Date
- Mar 2010
- Posts
- 13
running script bash to fill my files
Hi guys
I have around 600 empty text files that I need to add the name of this file as part of the data, I mean
files from "file1.txt to "file599.txt, all of them empty, and I need to get the name inside the file, so, when I open the file show the name as part of the data "file1".
these files were created on my web site, I am thinking in a small script in bash
Can somebody can help me with this?
Thanks
- 03-21-2010 #2
first type
function rename()
{
i=0;
for j in $*
do
mv $j file$i.txt
i=`expr $i + 1`
done
}
in the terminal
or copy this code.
then go to the directory where all the 600 files are located(if they are not in a directory move all of them to one directory).
then type
rename `ls`
thats all,your work is done.
it worked,i checked it.
copy the code i typed.
- 03-21-2010 #3Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
My understanding of your problem is that for files like the name:
you want the content of the file to be the name of the file, like:Code:file42.txt
Assuming this is correct, then consider this script:Code:file42.txt
which prodcues:Code:#!/usr/bin/env bash # @(#) s1 Demonstrate make name of file be written *to* the file. # Infrastructure details, environment, commands for forum posts. set +o nounset LC_ALL=C ; LANG=C ; export LC_ALL LANG echo ; echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG" echo "(Versions displayed with local utility \"version\")" c=$( ps | grep $$ | awk '{print $NF}' ) version >/dev/null 2>&1 && s=$(_eat $0 $1) || s="" [ "$c" = "$s" ] && p="$s" || p="$c" version >/dev/null 2>&1 && version "=o" $p set -o nounset echo # Remove old stuff, set up for test. echo " Initial conditions:" rm -f file*.txt touch file{1..3}.txt ls -lgG file*.txt echo echo " Results:" for f in file*txt do echo $f > $f done ls -lgG file*txt echo echo " Sample contents: file2.txt:" cat file2.txt exit 0
The inner for loop is the code that does the job, the rest is for setup, display, etc. You said that the files were empty, so this code does not try to save anything in the file; if there is any content in the files, it will be over-written.Code:% ./s1 Environment: LC_ALL = C, LANG = C (Versions displayed with local utility "version") OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64 Distribution : Debian GNU/Linux 5.0 GNU bash 3.2.39 Initial conditions: -rw-r--r-- 1 0 Mar 21 06:10 file1.txt -rw-r--r-- 1 0 Mar 21 06:10 file2.txt -rw-r--r-- 1 0 Mar 21 06:10 file3.txt Results: -rw-r--r-- 1 10 Mar 21 06:10 file1.txt -rw-r--r-- 1 10 Mar 21 06:10 file2.txt -rw-r--r-- 1 10 Mar 21 06:10 file3.txt Sample contents: file2.txt: file2.txt
I suggest that you first try this in a test directory, then you modify as necessary, keeping at least that for loop, and try it again. If it works, then run it for real in the directory of interest.
Best wishes ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 03-21-2010 #4Just Joined!
- Join Date
- Mar 2010
- Posts
- 13
- 03-21-2010 #5Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
If you change the echo inside the loop to:
then it will produce (excerpt):Code:echo ${f%%.txt} > $f
cheers, drlCode:Results: -rw-r--r-- 1 6 Mar 21 08:40 file1.txt -rw-r--r-- 1 6 Mar 21 08:40 file2.txt -rw-r--r-- 1 6 Mar 21 08:40 file3.txt Sample contents: file2.txt: file2
( Edit 1: fix typo in description ]Last edited by drl; 03-22-2010 at 09:31 AM.
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 03-26-2010 #6
Ok, I read your original post to mean "write the filename into the file, destroying any contents". Meaning, don't keep the data in the file as is, just load it with the filename.
Given that premise, here is my solution to your problem:
Now if you wanted to prefix the name onto existing data, its only a slight modification to preserve the file contents with a temp file.Code:#!/bin/bash for datfile in *.txt do bname=$(basename $datfile .txt) echo "$bname" > $datfile done
Code:#!/bin/bash tmpfile=$(mktemp /tmp/fixemXXXXXXXX) for datfile in *.txt do bname=$(basename $datfile .txt) echo "$bname" > $tmpfile cat $datfile >> $tmpfile rm $datfile mv $tmpfile $datfile done


Reply With Quote
