Jump to content
  • Announcements

    • spinynorman

      Mandriva Official Documentation

      Official documentation for extant versions of Mandriva can be found at doc.mandriva.com.   Documentation for the latest release may take some time to appear there. You can install all the manuals from the main repository if you have Mandriva installed - files are prefixed mandriva-doc.
    • paul

      Forum software upgrade   10/29/17

      So you may have noticed the forum software has upgraded !!!
      A few things that have changed. We no longer have community blogs (was never really used) We no longer have a portal page.
      We can discuss this, and decide whether it is needed (It costs money) See this thread: Here

Recommended Posts

I'm sure this is a silly question but I can't figure out how to unhide files via the shell or script. D4X hides partially completed downloads. It doesn't always unhide them properly when complete. I have tried using 'mv' and 'rename' without success. They both do not see the hidden files. Is there a way to get either one to see and rename the hidden files.

 

Thanks.

Share this post


Link to post
Share on other sites

As far as I know, the only thing that makes a file hidden in Linux is a decimal point in front of the name, i.e. .kde would make the kde directory hidden.

 

The way to see all hidden files is by simply using the:

 

$ ls -a

 

command. This will show all files that are in a directory. To unhide a file, simply remove the period at the start of the name.

 

I have not heard of any other way of hidding files in linux, but I may be wrong about that.

Share this post


Link to post
Share on other sites

You're right, the files do just have a dot at the beginning. I am able to list the files and can rename them through Konquerer. However, I'd like to rename them from a script. The 'mv' and 'rename' commands do not see them since they are hidden. Perhaps, there's a way to pipe the results of 'ls -a' into the 'mv' command?

 

I probably should point out that I'm trying to use wildcards. For example, the following commands would not find the hidden file: '.foo.bar'.

 

mv .* *

Edited by cptaylor

Share this post


Link to post
Share on other sites

mv .* * would move all files starting with . (wont move . and ..) to the next directory

 

eg) if you have a directory test/ and in test/ is a file .blah and directories test2/ and test/3

 

performing mv .* * in test/ will move .blah into the directory test2/

 

if you try mv .* * in a directory without any child directories it will result in an error about the last command must be a directory.

 

Even if you only have one .name file it will always think there are multiple files because every directory has . and .. which are for directory navigation i think, and because there are multiple files mv always wants the second argument to be a directory.

Share this post


Link to post
Share on other sites

If you want to do this is a script, you are probably going to have to come up with one that inspects each file in a directory, checks for a proceeding dot, assigns the name of the file to a variable and then strips the proceeding dot out of the name and uses the value of the variable in the move command.

 

I am not sure if shell script can do this, but something like perl, python or ruby could do it with ease.

Share this post


Link to post
Share on other sites

I'm trying to do it via the command line but its hard, you can't seem to assign variables from within a pipe, least its not working for me, besides can you even use variables for the source and destination in mv.

Will look into it further after i cook and eat dinner.

 

[john@bob test]$ ls -a | grep -i -h '^[[:punct:]]\w' | xargs -i sh -c "(echo {} | sed -e 's/\.//g'  -e 's/ /\ /g'; echo {})"
cat
.cat
cathat
.cat.hat
hat
.hat
[john@bob test]$

Edited by johnnyv

Share this post


Link to post
Share on other sites

You think thats long look at this ;)

but php is much much much much easier/faster to produce for me then sh scripts

 

create a text file call it mv_hidden

 

paste this into it:

#!/usr/bin/php -q
<?php

if($_SERVER['argc'] != 2)
{
die("You must enter a directory as an argument!\nUsage: mv_hidden <directoryname>\n");
}

$path = $_SERVER['argv'][1]; // retrieve the path argument
// check path and make sure it has a trailing /, if not add one ---------------------------/
$path_length = strlen($path); // length of path string
$path_length--;
$path_last_slash = strrpos($path,"/"); // last position of / character
if($path_last_slash === false)
{
$path .= "/";
}
else
{
if($path_length != $path_last_slash)
{
$path .= "/";
}
}
//------------------------------------------------------------------------------------------/
echo "Path to search: $path\n";
$counter = 0;
if($handle = @opendir($path))
{
while(false !== ($file = readdir($handle)))
 {
$type = filetype("$path$file");
 if($type == "file")
 {
	 if(strpos($file,".") === 0)
	 {
	 $new_name = substr($file, 1);
	 $command = shell_exec("/bin/mv $path$file $path$new_name 2>&1");
   if($command != "")// if we got an error display it
   {
   echo $command."\n";
   }
   else
   {
   echo "$file   -->   $new_name\n";
   $counter++;
   }
	 }
 }
}
if($counter != 0)
{
echo "$counter files converted\n";
}

closedir($handle);
}
else
{
die("'$path' is not a valid directory\n");
}
?>

 

save the file then make it executable, make sure php is installed, place the mv_hidden in your $PATH some where (perhaps /usr/bin)

 

Then cd to a directory you want to convert all .blah files to blah

The command is mv_hidden /path

Share this post


Link to post
Share on other sites

Oh by the way this is dangerous . files are usually configuration files so you can fsck up your box if you go messing with them.

YOU HAVE BEEN WARNED! :P

 

Oh and the script will overwrite files with the same name as your . file without the dot if you have write permission for that file.

 

eg .cat and cat

cat gets overwritten by .cat

Edited by johnnyv

Share this post


Link to post
Share on other sites

While the scripting can be fun and a good learning experience, have to ask why you are renaming the files? Period or no.. is there something wrong with their current file name?

Share this post


Link to post
Share on other sites

Would this not be better as some form of loop?? eg

 

#!/bin/bash

for OLDNAME in ./.*

do

NEWNAME=`echo OLDNAME | cut -d. -f3`

mv $OLDNAME $NEWNAME

done

exit 0

 

 

Or possibly using tr as opposed to cut.....

 

BTW hidden files are usually that for a reason! This script is untested, if you use it and it breaks something - YOU own the pieces.

Share this post


Link to post
Share on other sites
Guest AlgorithMan

I stumbled upon this thread because I want to do the same thing - my motivation is i want to hide/unhide empty/nonempty subdirectories of my ~/Videos directory (another script might put files into the hidden directories) so "not-doing it" or using GUIs is not an option... after reading through this thread I have to say, ALL proposals here are nonsense!

 

"cut -d. -f3" will mess up every file that has a name with more than one . in it

using php breaks butterflies on a wheel

"mv .* *" won't work at all (as pointed out before)

echo "$filename" | sed s/\.// would remove the first character from a filename - no matter, if it's a . or not (since \. is an arbitrary character)

echo "$filename" | sed s/\\.// would remove the first . from a filename - if the file isn't hidden it would remove a different . (e.g. the file-extension separator)

 

I wrote my own solution now - I also use sed, but only on ^\\. so it will only remove a . if it is the first character (this is indicated by the ^ )

using ^\. would remove the first character, since \. is an arbitrary character - combined with ^ would be the first character

 

mv "$f" "$( echo "$f" | sed s/^\\.// )"

 

this will unhide a file, but only if it has no directory in front of it... i think in that case

 

mv "$f" "$(dirname "$f")/$(basename "$f" | sed s/^\\.//)"

 

should do. Edit: yes, that one works nicely

Edited by AlgorithMan

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×