What does the wildcard (*) do when at the end of a file path

Questions about other topics - please check if your question fits better in another category before posting here
Forum rules
Before you post please read how to get help
Post Reply
Netherprovinc3
Level 4
Level 4
Posts: 305
Joined: Mon Feb 04, 2019 9:29 pm

What does the wildcard (*) do when at the end of a file path

Post by Netherprovinc3 »

I was reading this thread
https://www.reddit.com/r/archlinux/comm ... ove_cache/
The comment from user mtemmerm
gives a file path with the wildcard character at the end.

Code: Select all

~/.cache/*
Why does it need to use the wildcard character? Wouldn't this be the same thing?

Code: Select all

~/.cache/
Maybe just extra "notation" so as to avoid

Code: Select all

~/.cache
which would delete the .cache folder itself.
Linux Mint 19.1 Cinnamon unless otherwise noted.

User avatar
Flemur
Level 18
Level 18
Posts: 8016
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: What does the wildcard (*) do when at the end of a file path

Post by Flemur »

Netherprovinc3 wrote:
Fri Mar 13, 2020 2:57 am
I was reading this thread
https://www.reddit.com/r/archlinux/comm ... ove_cache/
The comment from user mtemmerm
gives a file path with the wildcard character at the end.

Code: Select all

~/.cache/*
Why does it need to use the wildcard character? Wouldn't this be the same thing?

Code: Select all

~/.cache/
Maybe just extra "notation" so as to avoid

Code: Select all

~/.cache
which would delete the .cache folder itself.
Kinda. You need to use either "rm -r" or "rmdir" to remove a directory.

Code: Select all

$ ls dir1  
file1  file2
$ ls dir1/*
dir1/file1  dir1/file2
$ rm dir1
rm: cannot remove 'dir1': Is a directory
$ rmdir dir1
rmdir: failed to remove 'dir1': Directory not empty
$ rm dir1/*
$ rmdir dir1
As far as copying (file=regular file, dir1=directory),

Code: Select all

cp file1 dir1
does the same thing as

Code: Select all

cp file1 dir1/.
IF dir1 already exists; if dir1 didn't exist, then you'd get a copy of file1 named dir1.

That's one reason why I always use the trailing "." If you think you're copying into a directory with

Code: Select all

$ cp aa dir1/.
and the directory doesn't exist, you'll get

Code: Select all

cp: cannot create regular file 'dir1/.': No such file or directory
rather than a copy of the file aa named dir1

Edit: The dot directory "." basically means "this directory", and ".." means the parent directory (the prompt below shows the current directory);

Code: Select all

/mnt/DATA/download : cd temp
/mnt/DATA/download/temp : cd . 
/mnt/DATA/download/temp : cd ..
/mnt/DATA/download : cd temp/.
/mnt/DATA/download/temp : cd ..
/mnt/DATA/download : cd temp/..
/mnt/DATA/download : 
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?

rene
Level 14
Level 14
Posts: 5288
Joined: Sun Mar 27, 2016 6:58 pm

Re: What does the wildcard (*) do when at the end of a file path

Post by rene »

Netherprovinc3 wrote:
Fri Mar 13, 2020 2:57 am

Code: Select all

~/.cache/*
Why does it need to use the wildcard character? Wouldn't this be the same thing?

Code: Select all

~/.cache/
On UNIX it's the shell that expands wildcards and, unless specifically configured to do so, does not include files/directories beginning with a dot in its expansion of "*". This is to say that if ~/.cache/ contains such files/directories (which it tends to not to) then no, the two aren't the same: former would leave said dot-files/directories untouched.

Which isn't to say that he/people might not tend to mean the same thing but still always be aware of that difference, for example wrt. backups. I.e., most people don't intend to miss e.g. ~/.thunderbird when copying ~/*.

Netherprovinc3
Level 4
Level 4
Posts: 305
Joined: Mon Feb 04, 2019 9:29 pm

Re: What does the wildcard (*) do when at the end of a file path

Post by Netherprovinc3 »

rene wrote:
Fri Mar 13, 2020 12:47 pm
Netherprovinc3 wrote:
Fri Mar 13, 2020 2:57 am

Code: Select all

~/.cache/*
Why does it need to use the wildcard character? Wouldn't this be the same thing?

Code: Select all

~/.cache/
On UNIX it's the shell that expands wildcards and, unless specifically configured to do so, does not include files/directories beginning with a dot in its expansion of "*". This is to say that if ~/.cache/ contains such files/directories (which it tends to not to) then no, the two aren't the same: former would leave said dot-files/directories untouched.
So

Code: Select all

rm ~/.cache/
would remove hidden files (these start with a .), right? Would it remove the . and .. ? I think those are in every folder (link to the current directory and parent directory).
Further, this command would not do anything with any directories in ~/.cache/

When you say in your post that this applies to Unix, are you just saying that because things can vary from shell to shell, and so you can only make so many generalizations?
Linux Mint 19.1 Cinnamon unless otherwise noted.

rene
Level 14
Level 14
Posts: 5288
Joined: Sun Mar 27, 2016 6:58 pm

Re: What does the wildcard (*) do when at the end of a file path

Post by rene »

Netherprovinc3 wrote:
Wed Mar 25, 2020 6:11 pm

Code: Select all

rm ~/.cache/
would remove hidden files (these start with a .), right?
Well, no. Plain rm ~/.cache/ would not in fact remove anything; would only complain about ~/.cache/ being a directory. It's the same as rm ~/.cache without the slash at the end. If you were to say rm -r ~/.cache or rm -r ~/.cache/ then this removes the entire directory ~/.cache, including any and all files and directories it in turn contains, hidden or otherwise (i,.e., yes, those starting with a period), although, no, not also ~/.cache/.., i.e., ~/, i.e., your home directory. . and .. you can best think of as "virtual entries"; handy shortcuts.

What I'm saying is that for a ~/.cache directory containing e.g. the two files ~/.cache/.foo and ~/.cache/bar the commands e.g. mv ~/.cache ~/tmp or mv ~/.cache/ ~/tmp do something quite different from mv ~/.cache/* ~/tmp: former two move the directory ~/.cache into ~/tmp if ~/tmp exists, rename ~/.cache ~/tmp if not, latter is expanded by the shell to mv ~/cache/bar ~/tmp before mv ever sees it. I.e., moves only the file ~/.cache/bar to ~/tmp, ignoring ~/.cache/.foo.

mv is not a perfect example here; neither is rm or cp due to those both needing an -r switch to operate on directories in the first place but with for example rsync you easily get into trouble if you are not aware of the mechanism (it tends to work to execute a command prefixed with echo if you are unsure of what it is going to do to see the command that the shell is after its own expansions going to execute).

Yes, things vary from shell to shell, and in this case in fact even potentially from configuration to configuration, but I said UNIX rather than Linux to in fact rather conversely emphasize that this is NOT all that variable over shells on any form of UNIX. In the old DOS COMMAND.COM and supposedly Windows' CMD.EXE shells, if you typed "SOMEPROG *.TXT" then the program SOMEPROG was in fact provided the string "*.TXT" as its one argument; would do any expansion of that wildcard itself under its own rules. Not so on UNIX; there the shell will expand the wildcard "*.TXT" and supposing e.g. that the current directory contains the files A.TXT and B.TXT and no other .TXT files, provide SOMEPROG with the two arguments "A.TXT" and "B.TXT", i.e., the actual command that would eventually be executed would be "SOMEPROG A.TXT B.TXT".

A lot of text; perhaps too much, but the question you ended up asking, whether or not ~/.cache/* and ~/.cache/ or ~/.cache were the same is to really be answered with "no". In the sense of former referring to a bunch of files and the latter two to one directory, i.e., the sense that Flemur also already sort of commented on, but also in that additional sense of * by default missing hidden files whether or not the first sense is relevant in some specific context in the first place.

Post Reply

Return to “Other topics”