Can we create file under a symlinked directory? - symlink

Say I have a directory called /data, which contains 3 files: a.txt, b.txt and c.txt
I am linking it to another directory using:
ln -s /data /this/is/temp
Then, if I want to create another file d.txt in /data, can I use the symlink directory instead?
touch /this/is/temp/d.txt

Yes, you can. Symlinks to directories can be used in paths just as if they were real directories.

Related

Getting permission denied while creating a directory and how to add suffix to .exe to each files in a directory

I want to create a directory say source_dir and add few dummy files without content into this directory like file1.txt, file2.sh, etc
Now I want to create another directory say destination_dir and move all the files from source_dir to destination_dir but all the files that got moved from source_dir should be suffixed by .exe
Example:
source_dir
file1.txt file2.sh
destination_dir should have output as
file1.txt.exe file2.sh.exe
What I have tried :
I used mkdir source_dir -> But getting error cannot create directory. Permission denied.
touch file1.txt file2.sh -> I thought to use this command to create the files without content but not able to create a directory itself.
Once error is resolved and files are created in source_dir then I will use
mv .* source_dir destination_dir -> To move all the files at once but for this command I am not sure whether this will work or not
Then how to suffix all the files with .exe is also challenging to me and got stuck.
Can someone help me resolving the error of create directory and how to add suffix to each files?
I used mkdir source_dir -> But getting error cannot create directory. Permission denied.
It seems you do not have permission to create a fodler here. You might use sudo mkdir source_dir, but is likely a better idea to make the folder in a directory where you have write access EG. $HOME.
Once error is resolved and files are created in source_dir then I will use mv .* source_dir destination_dir -> To move all the files at once but for this command I am not sure whether this will work or not
For moving use mv .* destination_dir from withing the source_dir. (IE, first cd source_dir then run the move command from above)
Then how to suffix all the files with .exe is also challenging to me and got stuck.
You will have to loop over the files and move them one by one.
for i in * ; do mv "$i" "${i}.exe" ; done

Copy files to their corresponding destination folders

I have many files in a folder with different extensions (.txt, .ascii, .mat). I want to move them to the destination folder which would be same as file names.
For example:
I have files like a.txt, a.ascii, b.txt, b.ascii, b.mat.
I want to make folder first in the name of a and b, then I want to move files a.txt,a.ascii to folder a and b.txt,b.ascii,b.mat to folder b.
I tried the code as follows. However I need an automatic way to make folder and move the files to it.
#!/bin/sh
mkdir a b
for file in $(<list.txt)
do
cp "$file" a
done
Seems that this Bash script can do the job
#!/usr/bin/env bash
for file in $(<list.txt); do
dirn="${file%.*}"
mkdir -p "$dirn"
cp "$file" "$dirn"
done

I want to move files in Shell

I have two folder xyz and Bin. I want to move xyz directory and it's sub directory as well to Bin folder.
I want output like this:
Bin/xyz/q.html
/images/0.gif
How can do move files and subdirectory and I will get that type of output.
Move (mv) can be thought of as rename. If mv is used to move a file from one drive to another, then a copy will be made and the original will be deleted.
If the file is a directory (folder), then all of the contents of that directory are also moved.
$ mv -v xyz Bin
moves the file called xyz, no matter what kind of file it is, even if it is a directory, into the existing directory called Bin. If Bin does not exist, then the move command will rename xyz to Bin.
If you only want to move a few files, for example, Bin/1.html is to be moved to Bin/xyz/1.html, then file being moved is a file called 1.html (not a directory called bin or xyz).
$ mv -v Bin/1.html Bin/xyz/1.html
will perform the move if the Bin/xyz exists and is a directory. If directory Bin/xyz does not exist, then this mv command will error because mv does not create parent destination directories.
$ mkdir -v Bin/xyz
will create an empty directory called xyz in an existing directory called Bin. Then files can be moved into the existing Bin/xyz directory.
The -v (--verbose) tells mv and mkdir to report what happened. The output that they produce given a success is what -v does. The most common way to list the content of a directory is ls, but the indentation that you desire will require some programming.
$ man mv
$ man mkdir
will show all the features of the mv an mkdir commands.

cp hundreds of files to different directories

The title not might be the best, I apologize, I'm rather new to scripting.
I'm trying to copy 2 files from each directory and place these compounds in a separate directory that only shares directory names.
for clarity;
/path/to/directory/all/variable_directories/
Inside this directory will be multiple files, I need 2 files which will have the same name in every individual variable directory.
I am trying to copy these 2 files from each individual variable directory and put them in variable directories based on the basename of /variable_directory/
the copy destination will be;
/path/to/magical/shit/subset/set_with_variable_name/variable_directories/
Only some of the destination directories are located in each /set_with_variable_name/
The script will need to be able to go through each /set_with_variable_name/ until it finds the directory that shares the basename of the directory that these files are originally being cp'd from
There's about 100 directories
to cp from and to and about 200 files total that need to be copied and sorted appropriately.
I can get it to cp ALL the files to the SAME directory using;
#! /usr/bin/env bash
for i in */;
do cd $i;
cp filename /path/to/destination/;
cp other_filename /path/to/destination/;
cd ..;
done;
It's the sorting the files to the correct destinations that I am completely lost at.
I appreciate any help, I'm a novice to this type of scripting
Looks like you need something like that.
# looping through all the variable directories
for var_dir in <path1>/all/*; do
# creating the destination directory if not exists
mkdir -p "<path2>/set_with_variable_name/$(basename ${var_dir})"
# copying the first file
cp "${var_dir}/filename1" "<path2>/set_with_variable_name/$(basename ${var_dir})/filename1"
# and the second
cp "${var_dir}/filename2" "<path2>/set_with_variable_name/$(basename ${var_dir})/filename2"
done

Make a copy of a file and give it a different name mac terminal

Mac.
I'm in a directory dogs/scripts/cats.
Within this directory there is a file bla.txt.
I would like to make a copy of bla.txt called bla2.txt and keep it in the same directory.
How do I do that?
cp bla.txt dogs/scripts/cats
'bla.txt' and `dogs/scripts/cats/bla.txt' are the same file
cp can get a name of a target file:
cp bla.txt ./bla2.txt
Or even simpler, as Mark noted:
cp bla.txt bla2.txt

Resources