command-line interview questions
Top command-line frequently asked interview questions
I am trying to print a text in the terminal using echo command.
I want to print the text in a red color. How can I do it?
Source: (StackOverflow)
I have a .sql file with an export from phpMyAdmin. I want to import it into a different server using the command line.
I have a Windows Server 2008 R2 installation. I placed the .sql file on the C drive, and I tried this command
database_name < file.sql
It is not working I get syntax errors.
How can I import this file without a problem?
Do I need to create a database first?
Source: (StackOverflow)
I am not sure whether it is possible to scp
a folder from remote to local, but still I am left with no other options. I use ssh to log into my server and from there I would like to copy the folder foo
to home/user/Desktop
(my local). Is there any command so that I can do this?
Source: (StackOverflow)
I've got a rake task that I am making that needs to insert a value into multiple databases.
I'd like to be able to pass this value into the rake task from the command line, or from another rake task, how can I do this?
Source: (StackOverflow)
I'm developing a C++ command-line application in Visual Studio and need to debug it with command-line arguments. At the moment I just run the generated EXE file with the arguments I need (like this program.exe -file.txt
) , but this way I can't debug. Is there somewhere I can specify the arguments for debugging?
Source: (StackOverflow)
How can I see the list of the stored procedures or stored functions in mysql command line like show tables;
or show databases;
commands.
Source: (StackOverflow)
I want to run two commands in a Windows CMD console.
In Linux I would do it like this: touch thisfile ; ls -lstrh
.
How is it done in windows?
Source: (StackOverflow)
Is there a way to include all the jar files within a directory in the classpath?
I'm trying java -classpath lib/*.jar:. my.package.Program
and it is not able to find class files that are certainly in those jars. Do I need to add each jar file to the classpath separately?
Source: (StackOverflow)
I'm looking for the string "foo=" (without quotes) in text files in a directory tree. It's on a common Linux machine, I have bash shell:
grep -ircl "foo=" *
In the directories are also many binary files which match "foo=". As these results are not relevant and slow down the search, I want grep to skip searching these files (mostly JPEG and PNG images). How would I do that?
I know there are the --exclude=PATTERN and --include=PATTERN options, but what is the pattern format? The man page of grep says:
--include=PATTERN Recurse in directories only searching file matching PATTERN.
--exclude=PATTERN Recurse in directories skip file matching PATTERN.
Searching on grep include, grep include exclude, grep exclude and variants did not find anything relevant
If there's a better way of grepping only in certain files, I'm all for it; moving the offending files is not an option. I can't search only certain directories (the directory structure is a big mess, with everything everywhere). Also, I can't install anything, so I have to do with common tools (like grep or the suggested find).
UPDATES: @Adam Rosenfield's answer is just what I was looking for:
grep -ircl --exclude=\*.{png,jpg} "foo=" *
@rmeador's answer is also a good solution:
grep -Ir --exclude="*\.svn*" "pattern" *
It searches recursively, ignores binary files, and doesn't look inside Subversion hidden folders.(...)
Source: (StackOverflow)
I am working on a simple scripting project for work that involves the use of Bash. I have a pretty simple script that is something like the following:
#!/bin/bash
VAR1="$1"
VAR2="$2"
MOREF='sudo run command against $VAR1 | grep name | cut -c7-'
echo $MOREF
When I run this script from the command line and pass it the arguments, I am not getting any output. However, when I run the commands contained within the $MOREF
variable, I am able to get output. I would like to know how one can take the results of a command that needs to be run within a script, save it to a variable, and then output that variable on the screen?
Source: (StackOverflow)
I've been using Remote Desktop Connection to get into a workstation, but I'm not able to use the shutdown/restart function in the Start menu while doing this.
I've put a few really helpful options in the answer below.
Note:
I wanted to make sure some really good answers were also mentioned along with my own on this.
And here they are in no particular order.
Source: (StackOverflow)
I am writing a batch file script using Windows command-line environment and want to change each occurrence of some text in a file (ex. "FOO") with another (ex. "BAR"). What is the simplest way to do that? Any built in functions?
Source: (StackOverflow)
Xcode 3.2 provides an awesome new feature under the Build menu, "Build and Archive" which generates an .ipa file suitable for Ad Hoc distribution. You can also open the Organizer, go to "Archived Applications," and "Submit Application to iTunesConnect."
Is there a way to use "Build and Archive" from the command line (as part of a build script)? I'd assume that xcodebuild
would be involved somehow, but the man
page doesn't seem to say anything about this.
UPDATE Michael Grinich requested clarification; here's what exactly you can't do with command-line builds, features you can ONLY do with Xcode's Organizer after you "Build and Archive."
- You can click "Share Application..." to share your IPA with beta testers. As Guillaume points out below, due to some Xcode magic, this IPA file does not require a separately distributed .mobileprovision file that beta testers need to install; that's magical. No command-line script can do it. For example, Arrix's script (submitted May 1) does not meet that requirement.
- More importantly, after you've beta tested a build, you can click "Submit Application to iTunes Connect" to submit that EXACT same build to Apple, the very binary you tested, without rebuilding it. That's impossible from the command line, because signing the app is part of the build process; you can sign bits for Ad Hoc beta testing OR you can sign them for submission to the App Store, but not both. No IPA built on the command-line can be beta tested on phones and then submitted directly to Apple.
I'd love for someone to come along and prove me wrong: both of these features work great in the Xcode GUI and cannot be replicated from the command line.
Source: (StackOverflow)
What's a Windows command line statement(s) I can use to get the current datetime in a format that I can put into a filename?
I want to have a .bat file that zips up a directory into an archive with the current date and time as part of the name, for example, Code_2008-10-14_2257.zip
. Is there any easy way I can do this, independent of the regional settings of the machine?
I don't really mind about the date format, ideally it'd be yyyy-mm-dd, but anything simple is fine.
So far I've got this, which on my machine gives me Tue_10_14_2008_230050_91
:
rem Get the datetime in a format that can go in a filename.
set _my_datetime=%date%_%time%
set _my_datetime=%_my_datetime: =_%
set _my_datetime=%_my_datetime::=%
set _my_datetime=%_my_datetime:/=_%
set _my_datetime=%_my_datetime:.=_%
rem Now use the timestamp by in a new ZIP file name.
"d:\Program Files\7-Zip\7z.exe" a -r Code_%_my_datetime%.zip Code
I can live with this, but it seems a bit clunky. Ideally it'd be briefer and have the format mentioned earlier.
I'm using Windows Server 2003 and Windows XP Professional. I don't want to install additional utilities to achieve this (although I realise there are some that will do nice date formatting).
Source: (StackOverflow)