I'm trying to create a command that allows me move a file to another directory. For example when I enter "move file1" in the command line, it should move the file "file1" to another directory. I know it can simply done as mv file1 /path/to/destination, But I want to create a new command. I'm kind of new Linux user, please help me.
This is what I tried:
Created an alias for move='/home/bin/move.sh'
So, now when I type move in the command line, it goes and execute move.sh script
Started writing a shell script move.sh as :
#!/bin/bash
mv "$2" "/path/to/destination"
I'm not knowing how to proceed. The whole process might be wrong too. Please help me solve this.
Thanks in advance
Create a function:
move () { mv -t /path/to/destination "$#" ;}
put it in ~/.bashrc to make it permanent.
Now run it as :
move /source /files
From here you can read on how to select arguments.
From here you can read more on how to check for number of arguments.
move.sh
#!/bin/bash
if (( $# < 2 )); then
# TODO: print usage
exit 1
fi
mv "$1" "$2"
Then you will need to make it executable.
chmod u+x move.sh
You can remove the .sh part. It wont change anything.
mv move.sh move
And then you should be able to call the file
move asd /home/
Just make sure that the alias calls the correct file.
If you want to make life easier delete the alias and place the file in the /bin/ directory
cp move /bin/
Good luck.
Related
I want to create a file abc.php within a directory a/b/c which does not exist yet. I need to do it with a single command like this mkfile a/b/c/abc.php I checked this solution which suggests solution like this mkfile a/b/c abc.php.
And along with this I would love to have the file opened in an editor which happens with nano command.
Usually while following tutorials we have to create file and copying and pasting their file directory without editing makes life simpler.
If I understand correctly and you simply want to be able to issue command foo/bar/baz/myfile.txt (or something similar) and have the directories foo/bar/baz created and a new file myfile.txt created and opened in nano all by that one command, then a short script is all you need, e.g.
Make it executable e.g. mv nanoopen.sh scriptname; chmod 0755 scriptname, then just call ./scriptname foo/bar/baz/file.txt. If you put it in your path, you can skip the ./ too.
The easy way to put it in your path is to create a symlink to it in /usr/local/bin which is generally in the default path.
So you could (sometime supersure is needed) ln -s /path/to/nanoopen.sh /usr/local/bin/scriptname. Echo $PATH to confirm /usr/local/bin is in your path, then just use it like any program, scriptname arguments.
Or in some distros you can simply add it to /bin folder with root access.
#!/bin/bash
[ -z "$1" ] && { ## validate one argument given
printf "error: insufficient input\nusage: %s filename\n" "${0##*/}"
exit 1
}
[ "$1" != "${1##*/}" ] && mkdir -p "${1%/*}" ## if it has directories, create
touch "$1" ## create the file
exec nano "$1" ## open in nano
Example Use/Output
$ bash nanoopen.sh foo/bar/baz/main.c
$ tree foo/
foo/
└── bar
└── baz
└── main.c
$ cat foo/bar/baz/main.c
My new source!
I need to touch a file with an absolute file name such as: /opt/test/test.txt, but I'm not sure if there is /opt/test existed on the system. So the code should similar with this:
if (-d '/opt/test') {
touch '/opt/test/test.txt';
} else {
mkdir -p '/opt/test';
touch '/opt/test/test.txt'
}
Is there any better way to simplify the code? I hope there is some system commands that can do the same job with only one line.
mkdir B && touch B/myfile.txt
Alternatively, create a function:
mkfile() {
mkdir -p $( dirname "$1") && touch "$1"
}
Execute it with 1 arguments: filepath. Saying:
mkfile B/C/D/myfile.txt
would create the file myfile.txt in the directory B/C/D.
In a shell script, you can simply do:
mkdir -p /opt/test && touch /opt/test/test.txt
mkdir -p will not fail (and won't do anything) if the directory already exists.
In perl, use make_path from the File::Path module, then create the file however you want. make_path also doesn't do anything if the directory exists already, so no need to check yourself.
In perl, using one of my favorite module: Path::Tiny.
path("/opt/test/test.txt")->touchpath;
From the doc:
Combines mkpath and touch. Creates the parent directory if it doesn't
exist, before touching the file.
I like typing very little, so I put this command into a named fn in my .profile, but I used this formulation for years before I did it:
mkdir -p dirname/sub/dir && touch $_/filename.ext
The variable $_ stores the last argument to the previous command. Pretty handy to know about overall.
I defined a touchp in my ~/.bash_aliases:
function touchp() {
/bin/mkdir -p "$(dirname "$1")/" && /usr/bin/touch "$1"
}
It silently creates the structure above the file if not present, and is perfectly safe to use when passed a single filename without any directory in front of it.
Perl from command line,
perl -MFile::Basename -MFile::Path=make_path -e'
make_path(dirname($_)), open(F, ">>", $_) for pop;
' /opt/test/test.txt
I have this shell function in my .zshalias file:
function touch-safe {
for f in "$#"; do
[ -d $f:h ] || mkdir -p $f:h && command touch $f
done
}
alias touch=touch-safe
If either the test or the mkdir command fail, no touch command is invoked.
Bring Python to command line.
i.e. Use pyp
cat filepaths.txt | pyp "'mkdir -p '+s[0:-1]|s+'; touch '+o" | sh
The Pyed Piper", or pyp, is a linux command line text manipulation tool similar to awk or sed, but which uses standard python string and list methods as well as custom functions evolved to generate fast results in an intense production environment.
I need a shell script to accept a directory-name and display its contents. If input is not given then HOME directory's contents should be listed. (This should make use of command line argument)
Please provide me some hints and solutions
With the condition of showing not the current directory but the home directory, this may be easiest to run in a script:
cd $1 && ls
if [ -z "$1" ]
then
ls ~
else
ls $1
fi
I'm having problems moving files into a folder after I create it in a shell script.
My script looks like:
#!/bin/bash
echo -e "Processing\033[36m" $1 "\033[0mwith the German script";
if [ ! -d ${1%.dat} ]; then
echo -e "making directory\033[33m" ${1%.dat} "\033[0msince it didn't exist...";
mkdir ${1%.dat};
fi
...processing occurs here... (irrelevant to issue)
if [ -d ${1%.dat} ]; then
mv useragents_$1 /${1%.dat}/useragents_$1;
mv summary_$1 /${1%.dat}/summary_$1;
more /${1%.dat}/useragents_$1;
else
echo -e "\033[31mERROR: cannot move files to folder.\033[0m";
fi
As you can see I create the folder if it doesn't exist in the top section and then if it exists I move the files into that folder in the bottom section, the problem is that it doesn't create the folder in time to move the files in (I'm assuming) so when it reaches the lower code, I only get the ERROR.
I tried using, sleep 5, but it only slows down the script and has no effect on the ERROR.
I would really appreciate some advice.
Errors below:
mv: cannot move `useragents_100_stns2_stns6.dat' to `/100_stns2_stns6/useragents_100_stns2_stns6.dat': No such file or directory
mv: cannot move `summary_100_stns2_stns6.dat' to `/100_stns2_stns6/summary_100_stns2_stns6.dat': No such file or directory
/100_stns2_stns6/useragents_100_stns2_stns6.dat: No such file or directory
Pass 1
Your check:
if [ ! -d ${1%.dat} ]; then
should be:
if [ -d ${1%.dat} ]; then
You created the directory; if it is a directory, move stuff into it.
Typo in question
Pass 2
You create:
mkdir ${1%.dat}
You try to move files:
mv useragents_$1 /${1%.dat}/useragents_$1;
Note the leading slash in the move compared to the create. Make those consistent.
Are you sure of this part ? It uses a root directory.
/${1%.dat}/summary_$1;
You probably want to do this instead:
${1%.dat}/summary_$1;
It allows you to move the file into the directory IN your current directory.
I started using "sudo rm -r" to delete files/directories. I even put it as an alias of rm.
I normally know what I am doing and I am quite experience linux user.
However, I would like that when I press the "ENTER", before the execution of rm, a list of files will show up on the screen and a prompt at the end to OK the deletion of files.
Options -i -I -v does not do what I want. I want only one prompt for all the printed files on screen.
Thank you.
##
# Double-check files to delete.
delcheck() {
printf 'Here are the %d files you said you wanted to delete:\n' "$#"
printf '"%s"\n' "$#"
read -p 'Do you want to delete them? [y/N] ' doit
case "$doit" in
[yY]) rm "$#";;
*) printf 'No files deleted\n';;
esac
}
This is a shell function that (when used properly) will do what you want. However, if you load the function in your current shell then try to use it with sudo, it won't do what you expect because sudo creates a separate shell. So you'd need to make this a shell script…
#!/bin/bash
… same code as above …
# All this script does is create the function and then execute it.
# It's lazy, but functions are nice.
delcheck "$#"
…then make sure sudo can access it. Put it in some place that is in the sudo execution PATH (Depending on sudo configuration.) Then if you really want to execute it precisely as sudo rm -r * you will still need to name the script rm, (which in my opinion is dangerous) and make sure its PATH is before /bin in your PATH. (Also dangerous). But there you go.
Here's a nice option
Alias rm to echo | xargs -p rm
The -p option means "interactive" - it will display the entire command (including any expanded file lists) and ask you to confirm
It will NOT ask about the recursively removed files. But it will expand rm * .o to:
rm -rf * .o
rm -rf program.cc program.cc~ program program.o backup?... # NO NO NO NO NO!
Which is much nicer than receiving the error
rm: .o file not found
Edit: corrected the solution based on chepner comment. My previous solutions had a bug :(
This simple script prompts for a y response before deleting the files specified.
rmc script file:
read -p "ok to delete? " ans
case $ans in
[yY]*) sudo rm "$#" ;;
*) echo "Nothing deleted";;
esac
Invoke thus
./rmc *.tmp
I created a script to do this. The solution is similar to #kojiro's.
Save the script with the filename del. Run the command sudo chmod a=r+w+x del to make the script an executable. In the directory in which you want to save the script, export the path by entering export PATH=$PATH:/path/to/the/del/executable in your '~/.bashrc' file and run source ~/.bashrc.
Here, the syntax of rm is preserved, except instead of typing rm ..., type del ... where del is the name of the bash script below.
#! /bin/bash
# Safely delete files
args=("$#") # store all arguments passed to shell
N=$# # number of arguments passed to shell
#echo $#
#echo $#
#echo ${args[#]:0}
echo "Files to delete:"
echo
n=`expr $N - 1`
for i in `seq 0 $n`
do
str=${args[i]}
if [ ${str:0:1} != "-" ]; then
echo $str
fi
done
echo
read -r -p "Delete these files? [y/n] " response
case $response in
[yY][eE][sS]|[yY])
rm ${args[#]:0}
esac