0
  1. Create 50 files whose names are from 1 to 50.
  2. Create five folders (0, 10, 20, 30, 40).
  3. Copy those 50 files into folders based on their name (for example, file 21 will go to folder 20).

I have created those files and folders, but have no idea how to move a specific file to specific folder. I tried writing

cp {1..9} /home/user/folder

but that's not acceptable. Is it possible to do using if and elif?

4
  • I'd use a simple for loop itc - but you can do without of course, but no need for if and elif as i see it Commented Dec 12, 2016 at 17:37
  • and in what folder should the file 50 go? in 40? Commented Dec 12, 2016 at 17:39
  • for i in {1..50} do if [ i -lt10 ] then cp $i.txt 0 Is it possible like this? Commented Dec 12, 2016 at 17:41
  • @DžiugasKubilinskas no its for i in {1..50} - see my answer below Commented Dec 12, 2016 at 17:42

2 Answers 2

1
cp {1..9} /home/user/folder

That will work, except you should specify the right folder to copy to. Perhaps you meant one of these?

cp {1..9} /home/user/folder/0
cp {1..9} 0

Another way is to use wildcards. * matches any string and ? matches any one character. 2? will match the file names 20 through 29, for instance.

Sign up to request clarification or add additional context in comments.

1 Comment

like this? : cp {2?} im only a begineer... :(
0

When you don't mind using a for loop instead of if and else you could use code like this:

#1
touch file{1..50}
#2
mkdir dir{0..40..10}
#3
for i in {1..4}; do
  cp file${i}* dir${i}
done
cp file* dir0 # note that this will copy file50 into dir0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.