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.
Related
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
I need to run a script in a remote machine from my JAVA code using runCommand() method. Now I can't always know the full path of the script as a particular directory name keeps changing. For example the path looks like this : /a/b/xxxxx/script . xxxx is the directory name that keeps changing and its the only single directory under /a/b/. Is there any shell command using which I can get the directory name ? I know using JAVA,but I specifically need shell command.
If there is only a single self-directory, another fool-proof way of doing it would be
cd */.
*/. is that this expands to the "self directory" (named .) in any subdirectory, which is of course the sub-directory itself. Refer the below example of how it works.
E.g.
$ pwd
/home/dude/
$ mkdir -p a/b/ldsnds/c
$ cd a/b/*/.
$ pwd
/home/dude/a/b/ldsnds
$ cd -
/home/dude/
$ cd a/b/*/./c
$ pwd
/home/dude/a/b/ldsnds/c
Below should give you the name of the directory in the directory "b".
$ find /a/b -type d -maxdepth 1 2> /dev/null
If you are so sure that it would always be one directory in /a/b then just store the output of find in a variable and move ahead.
Note: 2> /dev/null is just to get rid of errorneous warnings.
This is a follow up to https://apple.stackexchange.com/questions/52459/ and is about an unexpected behavior in bash. To summarize what's in that link, the problem is to copy the current directory in Terminal to a temporary variable, say the pasteboard, and use that to switch directory in a different Terminal window. The solution provided there pretty much nails it in the most efficient way! However, when I actually try changing directories using this temporary variable with the correctly escaped directory name, it seems to not work right in bash.
My minimum working example is as follows:
alias cwd='printf "%q/\n" "$(pwd)"'
Now in a terminal:
>$ mkdir tmp
>$ cd tmp
>$ mkdir test\ dir
>$ cd test\ dir
>$ cwd | pbcopy
In a new terminal:
>$ echo "$(pbpaste)"
/Users/foo/tmp/test\ dir/
>$ cd $(pbpaste)
-bash: cd: /Users/kaushik/tmp/test\: No such file or directory
>$ cd "$(pbpaste)"
-bash: cd: /Users/kaushik/tmp/test\ dir/: No such file or directory
I'm quite at loss in trying to figure out what I'm doing wrong. The only thing I'm certain of is that this is a bash problem and not something that's cropping up on OS X.
Thanks for your help on this and, by the way, it turns out that I had to finally, after all these many years, end up writing my first stack overflow post!
Copied from comments: The linked answer specifically asks for the escaped PWD suitable for pasting, but you want a programmatic input where escaping is counter-productive. Just do pwd | pbcopy and cd "$(pbpaste)".
EDIT:
(To be honest, I presumed you would need to escape it explicitly since that's how I create a directory with spaces using pwd.)
The issue is that command-line parser only does one pass of unescaping. In case of cd foo\ bar, the space is unescaped. In case of cd $(pbpaste), there is nothing to unescape; then pbpaste's literal output is put into the argument list.
I'm searching for just one command — nothing with && or | — that creates a directory and then immediately changes your current directory to the newly-created directory. (This is a question someone got for his exams of "linux-usage", he made a new command that did that, but that didn't give him the points.) This is on a debian server if that matters.
I believe you are looking for this:
mkdir project1 && cd "$_"
define a bash function for that purpose in your $HOME/.bashrc e.g.
function mkdcd () {
mkdir "$1" && cd "$1"
}
then type mkdcd foodir in your interactive shell
So stricto sensu, what you want to achieve is impossible without a shell function containing some && (or at least a ; ) ... In other words, the purpose of the exercise was to make you understand why functions (or aliases) are useful in a shell....
PS it should be a function, not a script (if it was a script, the cd would affect only the [sub-] shell running the script, not the interactive parent shell); it is impossible to make a single command or executable (not a shell function) which would change the directory of the invoking interactive parent shell (because each process has its own current directory, and you can only change the current directory of your own process, not of the invoking shell process).
PPS. In Posix shells you should remove the functionkeyword, and have the first line be mkdcd() {
For oh-my-zsh users: take 'directory_name'
Reference: Official oh-my-zsh github wiki
Putting the following into your .bash_profile (or equivalent) will give you a mkcd command that'll do what you need:
# mkdir, cd into it
mkcd () {
mkdir -p "$*"
cd "$*"
}
This article explains it in more detail
I don't think this is possible but to all people wondering what is the easiest way to do that (that I know of) which doesn't require you to create your own script is:
mkdir /myNewDir/
cd !$
This way you don't need to write the name of the new directory twice.
!$ retrieves the last ($) argument of the last command (!).
(There are more useful shortcuts like that, like !!, !* or !startOfACommandInHistory. Search on the net for more information)
Sadly mkdir /myNewDir/ && cd !$ doesn't work: it retrieves the last of argument of the previous command, not the last one of the mkdir command.
Maybe I'm not fully understanding the question, but
>mkdir temp ; cd temp
makes the temp directory and then changes into that directory.
mkdir temp ; cd temp ; mv ../temp ../myname
You can alias like this:
alias mkcd 'mkdir temp ; cd temp ; mv ../temp ../'
You did not say if you want to name the directory yourself.
cd `mktemp -d`
Will create a temp directory and change into it.
Maybe you can use some shell script.
First line in shell script will create the directory and second line will change to created directory.
I tried running a script file using bash but it showed an error
bash-3.2$ example.sh : command not found
I also tried
ls -l example.sh
I found that it was not executable, so I used
sudo chmod 777 example.sh
I again tried running it but same error was coming. I double checked that I am in the same folder as the file using ls. But still I am not able to execute the script file.
I finally tried making a dummy script file and running it , and found the same error
I think there is some problem with BASH. Can some one help me with what is the problem?
I am working on redhat, bash was already installed in my system
Since I am newbie on linux any help would be appreciated
bash search for commands in your $PATH. Apparently the current directory, ., is not in your $PATH. (This is a good thing; having . in your $PATH is insecure.)
You'll need to specify a directory name. Just type:
./example.sh
Incidentally, doing:
sudo chmod 777 example.sh
is two kinds of overkill. First, you don't need to use sudo; use sudo only when you actually need to. Presumably your personal account owns the file, so you can just use chmod directly.
Second, 777 is way too permissive. It allows anyone on the system to read, execute, or modify example.sh. (If you're the only person on the system it may not matter much, but it's still a bad habit.) Typically you should use 755 for directories and for files that need to be executable, and 644 for files that don't need to be executable.
Or just use
chmod +x example.sh
to set execute permission (your umask will prevent that from setting the permissions too loosely).
. (the current directory) is probably not on your path. Try ./example.sh or bash example.sh. You could also add . to your PATH environment variable, but that's generally frowned upon.
Your bash PATH probably doesn't include ., try running it by typing:
./example.sh
When you type a command, your shell searches your path to try to find the command, if the current directory (e.g. .) isn't part of the path, the script that you are trying to run won't be found. You'd have to explicitly give it the path to where this command is. And since it's in your current directory, you can just add ./ in front of the command.
first confirm the bash path
to check the path of bash use:
which bash
if you get "/bin/bash"
then add
#!/bin/bash
...
...
or whatever is the path on first line of your bash script