ls not displaying files anymore after rm a random file - linux

so i was using command prompt to make some code in C. Basically in the code I write an array to a file at the end of the code using fwrite() and the file is called output. Basically I wanted to remove "output" to see what would happened. So I did $ rm output. But now when I press $ ls nothing appears. If I go to another directory and use $ ls it is fine and $ ls actually shows all the files. However, $ ls does not work in the directory where I used $ rm output in. However, if I do $ vim FILE where FILE is a file I know is in the current directory, it actually shows me the contents. So I know it's still there and not deleted but it's not visible. So I tried to use WinSCP to try to move the folders. But then I got an error message that says
Can't get attributes of files 'DIR..'
Command 'ls -la -d "DIR.." ; echo "WinSCP: this is end-of-file:$status"' failed with invalid output ''.
where DIR is the path to my directory.
Does anyone know a fix to this it would be greatly appreciated.

I found the mistake. I mistakenly created a file called ls

Related

shell script mv is throwing unhelpful error "No such file or directory" even though i see it

I need to use a shell script to move all files in a directory into another directory. I manually did this without a problem and now scripting it is giving me an error on the mv command.
Inside the directory I want to move files out of are 2 directories, php and php.tmp. The error I get is cd: /path/to/working/directory/php: No such file or directory. I'm confused because it is there to begin with and listed when I ls the working directory.
The error I get is here:
ls $PWD #ensure the files are there
mv $PWD/* /company/home/directory
ls /company/home/directory #ensure the files are moved
When I use ls $PWD I see the directories I want to move but the error afterward says it doesn't exist. Then when I ssh to the machine this is running on I see the files were moved correctly.
If it matters the directory I am moving files from is owned by a different user but the shell is executing as root.
I don't understand why I would get this error so, any help would be great.
Add a / after the path to specify you want to move the file, not rename the directory.
You should try this:
mv $PWD/\* /home/user/directory/
Are your variables properly quoted? You could try :
ls "$PWD" #ensure the files are there
mv "$PWD"/* "/company/home/directory"
ls "/company/home/directory" #ensure the files are moved
If any of your file or directory names contains characters such as spaces or tabs, your "mv" command may not be seeing the argument list you think it is seeing.

what is the difference between . and `` operation in shell script

Request to need a help or information of the two operators . and `` in linux
e.g.
$ cp /home/uddi/root/hello `pwd`
and
$ cp /home/uddi/root/hello .
Please suggest me
A small difference occurs after mkdir /tmp/lost; cd /tmp/lost; rmdir /tmp/lost.
After these stupid commands pwd will be a filename (/tmp/lost) and the current dir . does not exist.
I think you want an error when you try to copy a file in the "current" dir, so I would prefer the .. It will also avoid an extra command.
When you enclose something between back-ticks, the shell will run the contents and use the output from that/those command/s as an argument for the main command being run. In your example, the shell will run the pwd command and use its output as the 2nd argument to the cp call.
In your second example, the . character is a link to the current directory. The reason that both do the same thing is that . links to the current directory and pwd will print out the current working directory, which are the same. In this case, you are using two methods to expand to the same path.
EDIT:
You can see somewhat how . works by running ls -a in any directory. It will show you the . and .. directories, which are filesystem-level links to the current and parent directory, respectively.

Bash - cat in a hidden . file or write to it

Let's say I make a file .history.txt:
touch .history.txt
and I try to write to it:
cat > .history.txt
after having done that all I get is:
bash: .history.txt: is a directory
What I need is to be able to write some text to it like I would be able to any normal file. Any ideas what am I doing wrong?
A file doesn't need to already exist in order to redirect output to it (the shell will create the file if necessary). But Bash is telling you that .history.txt already exists and is a directory, so you can't write to it.
You either need to remove the existing directory rm -rf .history.txt or use a different file name. Then cat > .whatever.txt should work on its own.

shell script : appending directory path and filename

I want to copy a file from a directory using shell script
Suppose I save the directory and file name seperately as
dir=/home/user/directory/
file=file_1
to copy the file Im using this command in my script
cp $dir$file .
But I get this error
/bin/cp omitting directory '/home/user/directory'
I have tried all combination eg. omitted the trail backslah from variable dir, etc but nothings working. I cant understand what is wrong with this code. Pleas help
Maybe the command $dir$file is not getting unpacked in the shell (ie only the directory variable is getting unpacked, not the file variable)!!!!!
It looks like you are having problem with expansion in cp $dir$file . In order to prevent possible problems, it is better to protect your variable with braces and double quote the full path/file to make sure you don't get caught by spaces in either the filename or heaven forbid the user's dirname:
cp "${dir}${file}" .
This will prevent the possibility the second $ is missed. Also make sure you have read access to other users /home (if you are root or using sudo you should be fine)
If you see this, when you somehow assign an empty string to file somewhere. Search your script for file= and unset file.
You can also debug this by adding
echo ".${file}."
in the line before the cp command. I'm pretty sure it prints .., i.e. the variable is empty or doesn't exist.

cp command simply not copying

I am working on a shell script and for some reason when I say
cp full_path/* full_path_directory/
I get an error. I have echoed out the command and when I run what it echos in an interactive shell it works. I can't figure out why it won't work in a shell script. I'm using full paths rather than absolute. I have tried to putting a slash at the end of the destination directory and then not putting a slash...what else could it be?
Error:
cp: /opt/local/apache2/htdocs/baseline/*: No such file or directory
So when I echo it out I get:
/opt/local/apache2/htdocs/baseline/* /opt/local/apache2/htdocs/test/
It means what it says. There are no files in /opt/local/apache2/htdocs/baseline/ directory, or you don't have permissions to read the directory. What does ls show you?

Resources