Mv or cp with a . (dot)?

How can I rename a file with a . prefix?
I actually need to copy the file but the . seems to be very tough to do.

mv ./bla ../fa/la/.bla - doesn't work. I've tried all sorts of bracketing and that doesn't work.

Maybe the only way to do it would be to name the file _.bla then rename it again immediately afterward to .bla?

Thanks.

Those names, dots included, are literal names. Do all the things involved exist (the source file and the target directories)?

./bla is a file named bla in the current directory (./)... not a file name .bla in the current directory. That would be .bla, or ./.bla.

There's nothing particularly special about files that begin with a dot (.).

When saying "it doesn't work", be specific about what you mean. e.g. the error you get from the mv command.

1 Like

In addition to what Scott has already said...

There are two things that are special about a filename starting with a period.

The ls command (without the -a option and without a filename pattern given as an operand that explicitly names the file) will not include those files in its output. (That is why files with names whose first character is a period are sometimes called hidden files.)

And, in the shell, filename matching patterns that do not begin with a period will not match a filename that has a first character that is a period. For example, if I am in a directory that contains the files . , .. , .hidden , abc , and xyz , then the command ls will produce the output:

abc	xyz

the command ls -a will produce the output:

.	..	.hidden	abc	xyz

the command echo * will produce the output:

abc xyz

the command echo .* will produce the output:

. .. .hidden

and the command echo * .* will produce the output:

abc xyz . .. .hidden
1 Like

That's all good information, but there's nothing special about files beginning with a dot when they're referenced directly - I should have made that distinction, as it was my point, in the context of the question.

I agree. I made a different assumption. My guess was that scribling didn't think that the command:

mv ./bla ../fa/la/.bla

succeeded because after running that command, a subsequent command:

ls ../fa/la

and didn't include .bla in the output.

Of course, all of this discussion is just guessing about what "doesn't work" meant in post #1 in this thread.

I figured it out. It may be convoluted but it works.

c=$(cd ../../bla/bla ; find . -name *.xyz) # Set variable c to filename (./filename.xyz)
c=$(c:3:23) # Remove "./" from c variable's filename
c=.$(c:0:26) # Add a "." to the variable filename

cp ../..bla/bla/*.zyz ./$c # Copy the file and rename it with c variable's name

The reason I need to do it this way is because I need the filename to retain it's original name but still add the . as a prefix.

---------- Post updated at 01:48 PM ---------- Previous update was at 01:43 PM ----------

The only reason I'm naming the files with a . is under mac OSX .files are invisible to the average user. I'm working at a place and the mgt here gets all pissy if they see a file somewhere they don't expect it to be, but for things to load automatically they need to be in a certain place ... So, I'm copying the files with the . prefix so I don't have to listen to, "Why is this here?"

Come to think of it I don't know why they ask that question. They don't care why they're there and only want me to delete them regardless of the reason.

That's a terrible solution, if you can consider it a "solution" at all (I got tired reading it half way through).

And "security by obscurity" thinking, as it would appear you intend, by hiding files (with a dot, that only an idiot wouldn't see, who'd magically think 'that's nice and clean') is no "security" at all.

Having said that, a company I used to work for once shipped a CD of production software to a big client with core dumps in many directories, albeit old ones, so your caution is understandable!

Files that begin with a dot are typically configuration-related. If you generate temporary files along the way, simply remove them when you're done. I, too, might ask 'what is this file' if I saw one that shouldn't be there, but I wouldn't get "pissy" about it until the next time I did a df, or got an alert, and saw a potential issue.

I'll get my coat.

I was quite proud of it. It works fast and efficiently. BTW there is no security issue here. I work in visual fx. We use files called cdl (color decision list) ... well if the cdl file is in the parent directory of the files you're viewing it'll load the cdl automatically. If not, you have to go find it and move it in manually. Originally, I put copies of the cdl files where they need to be and next thing I know there's a company wide email about keeping the file structure clean and cdl files don't belong in the ... So, now I just hide them. They're still there and they work just fine with the . prefix but these people can't see them and therefore don't complain. If you heard them go on and on about you'd think I rearranged the living room of a blind person. I have to do the same with any symlinks and aliases as well.
"What is this?"
An alias ...
"Why is it here?"
So you can go to that directory quickly and easily ...
"Who's it for?"
Anyone ...
"Well, we don't use these here!"
It's not hurting anyone ...
"We need to keep things consistent!"
What is the problem?

This is what I'm dealing with.

2 Likes

Thank you for cheering me up with that story.. and for reminding me that not everything is black or white.

It drives me crazy when I start a new job and they say, "This is how we do ..."
I sit and watch and when they finish I usually say, "You do that every time, with every file?"
"Yes!"
"I could easily write a script that'll do all that stuff in about 5 seconds ... and it won't make mistakes."
"THIS IS THE WAY WE HAVE ALWAYS DONE IT HERE! And we want you to do it that way."
"Oh, I'll do it the same as you do but I'm not going through all that ..."

Usually, after a few days the other artists are asking me, "How do you do that so fast?"
"I wrote a script. These are computers! They're made to do repetitive tasks quickly and easily." What is wrong with you people?!

So, back to the original question, is that a dumb way to copy the file and rename it with a . prefix? If there's another way, I'd love to hear it.

I've worked in a bank.. you're preaching to the converted lol.

I'm not sure that the original way we showed you was wrong, so...

Note that the following code:

c=$(cd ../../bla/bla ; find . -name *.xyz) # Set variable c to filename (./filename.xyz)
c=$(c:3:23) # Remove "./" from c variable's filename
c=.$(c:0:26) # Add a "." to the variable filename

cp ../..bla/bla/*.zyz ./$c # Copy the file and rename it with c variable's name

won't work if no files match the pattern and won't work if more than one file matches the pattern ../../bla/bla/*.xyz . The following should work no matter how many files in that directory match:

find ../../bla/bla -name '*.xyz' | while read -r c
do	cp "$c" ".${c##*/}"
done

If you want it to keep you informed about what it's doing, either add a set -x to the start of the script or add an echo "$c" before the copy command in the loop.

P.S. Note however that if there is a hidden file in your source directory that ends in .xyz you'll end up creating a file with two leading <period>s in your current working directory.

1 Like

Actually, I wanted it to fail if no files matched.

I like the cp "$c" ".${c##*/}" idea. I'll try that.
Thanks!

Thanks for allowing me to vent. My wife can't comprehend. I told her it's as if someone was still using the pony express because "That's the way we've always done it!" I'm still not sure she gets it. I find that after a certain age many people literally refuse to learn anything new.

Even better... Now you can get rid of the find too:

for c in ../../bla/bla/*.xyz
do	cp "$c" ".${c##*/}"
done

If there aren't any matching files, you'lll get a diagnostic from cp saying something like:

cp: ../../bla/bla/*.xyz: No such file or directory

and a non-zero exit from your script.

P.S.: Note that (as mentioned before) this code won't copy hidden files.

I do not think fear of change is closely related to age.

People are inherently afraid of changes.
Changes like jobs, partners, living space etc.
The IT is no different.

Problem nowadays is that a good programmer / engineer can cause a lot of former jobs redundant.

And in this wild capitalism company will not see those people as an asset, but as a money drain.
They will not re qualify / re educate those people to do something else.

Best example are self driving trucks and cars.
In about 5 to 10 years, self driving trucks will be common.
What will all those truckers do then ?
Most of them will not adapt and become a social burden.

Progress ? Yes!
But not all variables have been included or discussed at higher levels.
Not all people are meant to code or build machines.
I'm sure automation and robotics experts didn't have more poverty in mind when designing the next big thing.

Sorry for the philosophical rant!

Regards
Peasant.

4 Likes

I'll try that newer code. I'm sure it'll work great. Thanks again.

I find it funny that I know so little about simple coding like this and a lot of people think I'm some sort of genius. If only I had the knowledge you guys have - I could be even more frustrated! Knowing what can be done and then having to fight with people and their status quo is a mental battle for me. Do I try to convince them or do I leave them alone? I usually do things my way, keep it to myself, and after a while they hear about it and want to see what I've written and how it works, but almost no one is willingly open at first.

---------- Post updated at 09:21 PM ---------- Previous update was at 09:08 PM ----------

Oddly, what you're talking about with automation I agree with somewhat. The self driving cars will need someone to program them, someone to build them, someone to get them when they breakdown ... I've noticed that here in America everyone wants a fantastic job and that's who they are. In other countries people have a job and no matter what it is it's a job and they're proud to have A job at all. Most Americans, it seems, would rather accept free money from the government than work a job they don't think is worthy of their efforts.

I recently watched an episode of Anthony Bordain's Parts Unknown where there were dozens of people who's job it was to slowly turn a spit while a pig cooked. No automation. These people crouched down and slowly turned that handle, hour after hour. They had jobs!

We need to change our culture from, "free money is better than working", to "If you didn't work for your money you're the scum of the earth." Instead of people being embarrassed about a shitty job they should be embarrassed they don't have a job at all.

Or the other way round, you are in the source dir and want to copy to the ../fa/la/ dir

for c in *.xyz
do
  cp "$c" "../fa/la/.$c"
done

There is nothing basically wrong with your command. See the transscript from my shell:

-1-2- ~/tmp > mkdir ../tmp/abcd
-0-2- ~/tmp > touch bla
-0-2- ~/tmp > mv ./bla ../tmp/abcd/.bla
-0-2- ~/tmp > ls -l ../tmp/abcd/.bla
-rw-r--r-- 1 fisrona Domain Users 0 Dec  1 08:26 ../tmp/abcd/.bla
-0-2- ~/tmp >

You say doesn't work without any additional information. I take it that you didn't get an error message from mv, otherwise you would have posted it. Now I have never encountered a case where mv would not work and at the same time does not write a message to stderr to explain why it failed.

I suggest that you reproduce the commands which I have posted on your machine and compare the output.

I appreciate that I am coming to this thread very late and you seem to have some very qualified expert help here already. Therefore, I think this is likely to be my only post on this thread. However, I would like to contribute the following notes:

  1. You are confusing filename and path. In /a/b/c/xyz , the /a/b/c is the path and xyz is the filename. The leading / means the path starts from the top of the system.

  2. Without the leading / the path starts from the directory you are currently in, ie, there is a directory a pre-existing, with a b directory under that pre-existing, and a c directory under that pre-existing.

I don't think you have told us all what OS you are using but it's unlikely that your (failing) 'mv' command is capable of creating the directories 'fa' and 'la' whilst moving the file. I think your destination path MUST pre-exist before you issue your 'mv' command.

I just wanted to make that point. That may, or may not, be your issue.

Wow, you're right. I completely wrote that command backward in the original post. It should have been mv ../fa/la/bla ./.bla

I got great feedback even though I completely screwed up the question.

Thank you everyone for the great info.