LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-27-2019, 08:59 AM   #1
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Rep: Reputation: 56
Insert word between filename.


Hello

How to insert a word in between a filename with spaces while cp or mv. For example in the following two examples, I do as follows:

Code:
# ls -l /etc/vsftpd/vsftpd.conf*
-rw-------. 1 root root 5113 Dec 24 18:30 /etc/vsftpd/vsftpd.conf

# cp /etc/vsftpd/vsftpd.conf{,.backup}

# ls -l /etc/vsftpd/vsftpd.conf*
-rw-------. 1 root root 5113 Dec 24 18:30 /etc/vsftpd/vsftpd.conf
-rw-------. 1 root root 5113 Dec 27 20:12 /etc/vsftpd/vsftpd.conf.backup
Code:
# ls -l /etc/vsftpd/vsftpd*
-rw-------. 1 root root 5113 Dec 24 18:30 /etc/vsftpd/vsftpd.conf

# cp /etc/vsftpd/vsftpd{,.PREFIX}.conf

# ls -l /etc/vsftpd/vsftpd*
-rw-------. 1 root root 5113 Dec 24 18:30 /etc/vsftpd/vsftpd.conf
-rw-------. 1 root root 5113 Dec 27 20:15 /etc/vsftpd/vsftpd.PREFIX.conf
Following this method, how to insert some chars between filename with spaces.

For example, I want to copy the file 'A Sample File.txt' as 'A Sample TEST File.txt'

I tried these, won't work.

Code:
# cp 'A Sample {,TEST} File.txt' 
cp: missing destination file operand after 'A Sample {,TEST} File.txt'

# cp 'A Sample \{,TEST\} File.txt' 
cp: missing destination file operand after 'A Sample \{,TEST\} File.txt'
Thanks
 
Old 12-27-2019, 09:53 AM   #2
fatmac
LQ Guru
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: Mainly Devuan, antiX, & Void, with Tiny Core, Fatdog, & BSD thrown in.
Posts: 5,493

Rep: Reputation: Disabled
Linux doesn't like spaces in file names, it's a separator.
Replacing the space with an underscore is the easiest way.
Your problem is mainly owing to a lack of destination file, mv & cp, from/to.
Code:
mv A_Sample_File.txt A_Sample_TEST_File.txt
 
Old 12-27-2019, 10:18 AM   #3
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,600

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
The reason why what you tried is erroring is because brace expansion doesn't occur inside single-quoted strings, meaning you're only passing a single argument (which is why the messages complains about a lack of destination).

Remove the quotes and backslash escape the spaces and you will get the behaviour you're after -- however, in the example you posted, you have spaces eitherside of the braces - meaning your first file would be A(space)Sample(space)(space)File.txt (which presumably does not exist). You most likely want one of the escaped spaces inside the braces so it's only added when the prefix is.
 
1 members found this post helpful.
Old 12-27-2019, 10:56 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
this is definitely not the way, but if you wish:
Code:
cp /tmp/{"A Sample File.txt","A Sample TEST File.txt"}
 
Old 12-27-2019, 11:15 AM   #5
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Original Poster
Rep: Reputation: 56
Quote:
Originally Posted by boughtonp View Post
The reason why what you tried is erroring is because brace expansion doesn't occur inside single-quoted strings, meaning you're only passing a single argument (which is why the messages complains about a lack of destination).

Remove the quotes and backslash escape the spaces and you will get the behaviour you're after -- however, in the example you posted, you have spaces eitherside of the braces - meaning your first file would be A(space)Sample(space)(space)File.txt (which presumably does not exist). You most likely want one of the escaped spaces inside the braces so it's only added when the prefix is.
Worked, Thanks.

Code:
$ cp A\ Sample{,\ TEST}\ File.txt
Thanks
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
regex for phrase like'word-word-word' Zero4 Linux - General 9 07-06-2019 06:36 AM
How to find a specific word between two lines having same another word.. say_hi_ravi Programming 4 04-18-2013 06:37 PM
to extract all the part of the filename before a particular word in the filename aealexanderraj Programming 1 08-27-2012 11:08 AM
filename- and filename~ files? slinky2004 Linux - Newbie 5 10-17-2004 10:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration