Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How would one go about taking all the *.txt files in a given folder (users input) and copying odd lines to files with identical names but different extension (bat/html or anything else)

I assume cp or mv wont work, something like sed should do the trick for lines, but im having a hard time incorporating everything together.

If anyone could help me piece this together I would be very thankful


#seems like a good idea to get a full path to the directory im gonna be working in, not #sure how to go about it though. 


#getting users input

read Directory

#check if said directory exists
if[ -d $Directory]; then
#if it does
#all files with .txt extention are to be copied 
for *.txt in $(ls)
mv *.txt *.bat

#aaand now im lost, didnt have a chance to even test this, since i have linux only in #my studying enviroment (uni), will get on my personal PC later on
#something like sed could/should work in there, but i have no idea how to go about it

#if doesnt
echo "Directory does not exist or you do not have a permission to alter its contents"
share|improve this question
Do you want to move the odd lines into one file or into different files? – minerz029 Dec 16 '13 at 7:45
I want to copy odd lines into different files. For example 1.txt -copy odd lines to-> 1.bat – user225827 Dec 16 '13 at 9:06
up vote 3 down vote accepted
cd "$1" || { echo "Couldn't cd to directory $1.  Quitting." ; exit 1 ; } 
for fname in *.txt
    awk 'NR % 2 == 1 { print; }' "$fname" >"${fname%.txt}.bat"

Put the above in a file and make the file executable (chmod +x yourfilename). Now, run this command with the directory that you want to operate on as the first argument.

The script changes to the directory you request (cd "$1"). If that fails (the directory doesn't exist), then the script exits with an error message. If cd "$1" succeeds, then the script goes through each .txt file in the directory, selects the odd lines (NR % 2 == 1) and writes them to a file with same name except with extension ".bat".

share|improve this answer
Thanks! Seems so simplistic now that i look at it Have to look up awk, since im unfamiliar with this command – user225827 Dec 16 '13 at 9:10

Not sure what you mean by "odd lines"[1], but this statement achieves just what your code currently attempts to do:

cd "$1" && rename 's/\.txt$/.bat/' *.txt

No need to do your own error messages, the shell will do them just fine.

[1] This makes it sounds a lot like a Uni homework assignment.

share|improve this answer
odd lines from the .txt files im renaming. Pretty much is, had C all semester that i was acquainted with prior to uni, so completed all C assignments, got a scripting one to get familiarized with Unix. Lots of things to learn, and after C/C++ and some Assembly, doing things globally by type, instead of looping through all elements one-by-one is a new concept that i need to get used to. Cheers! – user225827 Dec 16 '13 at 9:18

Thank you to all that have replied, you have helped me greatly in gaining some understanding of bash scripting, now that this question is resolved, how would one go about closing a question?

I used script provided by John1024, worked flawlessly. Very thanful for that. Cheers


cd "$1" || { echo "Couldn't cd to directory $1. Quitting." ; exit 1 ; } for fname in *.txt do awk 'NR % 2 == 1 { print; }' "$fname" >"${fname%.txt}.bat" done

share|improve this answer
If the answer given by @John1024 worked for you then please accept it rather than adding your thanks as an "answer" - see – steeldriver Dec 19 '13 at 22:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.