ubuntu mv file disappeared - file

On ubuntu, I have the following directory structure:
/var/www/html
I am in www and I decided to move a file from this directory to html. I executed the following:
mv myfile.iso /html
Now I cannot see the file. Apparently I did not use the command properly. Any help to recover the file?

Yeah, your file is either now called html, and it stands in the root directory / (I'm not even sure this is possible), or it moved to existing directory /html
You must do the following to find it back (second case I spoke of):
mv /html/myfile.iso /var/www/html
or if it doesn't work (first case):
mv /html /var/www/html/myfile.iso

When u need to move a file you need to do this like: "mv foo ~/Desktop",
You forgot the ~ sign. What mv does is also rename the file.
The mv command will move a file to a different location or will rename a file. Examples are as follows: "mv file foo" will rename the file "file" to "foo". "mv foo ~/Desktop" will move the file "foo" to your Desktop directory, but it will not rename it. You must specify a new file name to rename a file.
You should look if u can find a folder called html in the root.

Related

Making fopen() open files from a certain directory

I have a function with something like
FILE *file1 = fopen("testing.txt", "r");
I can't modify this line. However, if I make a file named "testing.txt" in, say /tmp, would I be able to make the function load the file from /tmp instead of it's own directory. (Maybe by modifying the PATH variable?)
If the program doesn't change its own working directory, you could cd into /tmp and simply run the program from there.
$ cd /tmp
$ /absolute/path/to/my_program
That opens a file from your current working directory.
You can change the current working directory using chdir.
See this.
This is using C code.
You can also use cd.
For example, go to the terminal:
$ cd /tmp
$ cd /path_to_your_program
Also, cd .. will make you go to the directory above, and cd will make you go to the home directory.
Also, if you do not have the program in the directory in which you have to compile it, you can use cp which copies file.
$ cp /path_to_copy_from /path_to_copy_to
Then you can go to that directory, and run it from there.
I would recommend you to take a basic linux tutorial like this.

Copying files using a manifest as reference

I need to move a bunch of files in the /data/files directory to a new folder structure. In the /data directory, I have a manifest file that contains the full working path of each file.
Source Files:
/data/files/this_file_1.sh
/data/files/this_file_2.sh
Manifest File Contains:
/path/to/where/file/goes/this_file_1.sh
/a/different/path/to/file/this_file_2.sh
I need help writing a shell script that will find the filename in the manifest and run a copy command. If the directory structure doesn't exist, create it.
#!/bin/bash
for file_name in $(cat manifest); do
name=$(basename $file_name);
path= ??
# Mkdir / Copy
done
How can I get the filename from the full path of the file?
How can I create the directories prior to copying the files?
I'm going to let you write the full script, but here are some commands you could use:
"Find the filename in the manifest" - basename filepath
"Verify it exists in /data/files" - if [ -e filename ]; then ......
"Run a copy command" - cp location1 location2
"If the directory structure doesn't exist, create it" - else mkdir /data/files or whatever the directory you want to create is.
If you have any questions about these commands leave a comment :-)

remove multiple files using terminal?

I want to delete two specific folders and a file from my current directory:
1. settings/
2. models/
3. file.txt
How do I remove these using single command in terminal?
rm -rf settings/ models/ file.txt
The solution provided by #jcpennypincher is correct for unix system since folders are functionally treated like files.
r stand for "recursive": delete files in directory and in its subdirectories and in its subdirectories etc...,
f stand for "force".
man rm
If you are looking for the same solution in windows (command prompt) it can't be done in just one command: files and folder in windows have different rappresentations. You have to use del for files and rmdir command for folders.

Moving/Grouping Files Unix

I have one folder with about 1000 files and I want to group them according to their resepctive parent folders.
I did ls- R > updated.txt to get the original setup of folders and files.
The updated. txt looks like this:
./Rhodococcus_RHA1:
NC_008268.fna
NC_008269.fna
NC_008270.fna
NC_008271.fna
./Rhodoferax_ferrireducens_T118:
NC_007901.fna
NC_007908.fna
./Rhodopseudomonas_palustris_BisA53:
NC_008435.fna
./Rhodopseudomonas_palustris_BisB18:
NC_007925.fna
./Rhodopseudomonas_palustris_BisB5:
NC_007958.fna
./Rhodopseudomonas_palustris_CGA009:
NC_005296.fna
NC_005297.fna
So, by looking at this file, I know what files go into what folder. The folder with all the 1000 files together looks like this:
results_NC_004193.fna.1.ebwt.map
results_NC_004307.fna.1.ebwt.map
results_NC_004310.fna.1.ebwt.map
results_NC_004311.fna.1.ebwt.map
results_NC_004337.fna.1.ebwt.map
results_NC_004342.fna.1.ebwt.map
results_NC_004343.fna.1.ebwt.map
results_NC_004344.fna.1.ebwt.map
and so on...
You can see that the filenames of all the 1000 files are dependent on their original names in the folder setup(if that's a good way to explain it).
I want to move these results_XXXXXXXX files to folders (have to create new folders) with the original setup. So it should be something like this:
./Rhodococcus_RHA1: (this is a folder)
results_NC_008268.fna.1.ebwt.map
results_NC_008269.fna.1.ebwt.map
results_NC_008270.fna.1.ebwt.map
results_NC_008271.fna.1.ebwt.map
./Rhodoferax_ferrireducens_T118:
results_NC_007901.fna.1.ebwt.map
results_NC_007908.fna.1.ebwt.map
I don't really know how to do this... maybe some kind of mov command? I'd appreciate help with this problem.
Run the following command from the folder where you have those 1000 files. The path/to/original/files is the path to the original files (the one that you did ls -R). you should get a list of mv commands. Verify several of them to confirm that those are correct. If so, add | sh next the command and rerun it to execute those commands. If you don't have all the corresponding files in the 1000 files folder, you would get mv commands that would return "file not found", that can be ignored or piped to /dev/null. This assumes that you always have a file in original folder so that it knows where to move the file. If not, some of those 1000 files won't be moved. As always, take a good backup before you do this.
find path/to/original/files -type f | awk -F"/" '{ path=$0; sub($NF, "", path); printf("mv results_%s.1.ebwt.map \"%s\"\n", $NF, path);}'

Creating makefile for C code, running without ./

I need to create a makefile that will compile my simpleprogram.c to sp and it can be called like unix commands like ls,ps etc, without writing explicitly ./sp. I looked upon the web and cannot find a solution, or searching it in a wrong way. I cannot search like "executable without ./" , because I do not know what is this called => "./"
Put the binary in a directory that's in your PATH.
http://www.cs.purdue.edu/homes/cs348/unix_path.html
Just copy your program to your systems bin (executable binaries) directory.
Most commonly its /usr/bin for programs which can be used by all user.
If the app is only for admins, you should use /usr/sbin/ directory.
Remember to set the "executable" flag with chmod: chmod +x your_app
The proper solution to this (assuming you don't want sp to be run from outside of your makefile) is to call your program using the full path name instead of ./ (which is relative, and can change during multi-directory makes). In your makefile do something like:
SP_DIR := $(shell pwd)/spdir
rule : somedependency
$(SP_DIR)/sp
Where $(shell pwd) will expand to the directory the makefile is being run from. If your sp directory is in a parent directory of this, it is possible to use .. in the path as well: eg.
SP_DIR := $(shell pwd)/../../spdir
If you do want to run sp from outside of the makefile, then you need to either copy sp to a directory specified in your PATH variable (do echo $PATH to see these), or modify your .bashrc or equivalent file to make PATH include the directory that sp is built in.
John
You can just do:
export PATH=$PATH:.
But this is not a good idea, in general.

Resources